Doubt with Routes and Controllers Laravel 4

Asked

Viewed 120 times

3

I’m a beginner in Laravel and I’m having a problem with routes and controllers. I created a form with the following action:

{{ Form::open(array('action' => 'MatriculasController@formulario' }}

I created a route for this action:

Route::post('formulario', 'MatriculasController@formulario');

The Matriculacontroller form method does some operations with the data received from the form and should return a View:

    //função que recebe e manipula os dados do formulario
public function formulario(){
    //recebe os dados do formulario
    $ra=$_POST['ra'];
    $aluno=$_POST['nome'];
    $nasc=$_POST['nascimento'];
    $responsavel=$_POST['responsavel'];
    $cpf=$_POST['cpfResponsavel'];  

    //verifica se o RA foi fornecido
    if (strlen($ra>0)){
        $siga = new Siga();
        $consultaRA=$siga->conecta("select NOM_PESSOA from supervisor.PESSOA where COD_PESSOA='$ra'");
        $resultado=mssql_fetch_array($consultaRA);
        $nome=utf8_encode($resultado['NOM_PESSOA']);
        return View::make('confirmaIdentidade');
    }
    .
    .
    .
}

The View confirms identity:

@extends('templates.templateVeterano')

@section('conteudo')

teste

@stop()

The problem is that it is not returning this view and I do not understand the reason. It is returning a blank page with the url /form.

Does anyone know what I might be doing wrong?

  • Amanda Lima, has happened to me a lot. In order to help you, you will have to post part of your controller. At least the methods involved

  • 1

    There could be many things: You’re using a Section that doesn’t exist in the parent view. You’re accessing a method that doesn’t return anything.

  • I’ll edit and post all the code

  • 1

    +1 Because I love Laravel!

  • 2

    If I had known better, I’d have hit you like a cousin, huh!

1 answer

2


Amanda, it turns out that what brings back the blank page in your code is the View::make that’s inside the if.

If it does not meet the condition, as it is not returned, then your code will return a blank page.

To check if this statement is correct, try this after the if of $ra.

return 'Alguma coisa errada';

If this appears, then the reason stated above is correct.

What can be done - is what I would do - is to put the return View::make out of any condition if. If there is a need to send an error message, you can do so:

if ($erro) {
     // Como a requisição é post, volta para mesma página
     // com uma mensagem flash na sessão
     return Redirect::back()->with('mensagem', 'Mensagem de erro');
}

return View::make('x');

In view:

@if(Session::has('mensagem'))
   {{ Session::get('mensagem') }}
@endif

Another problem that may cause the blank page on Laravel 4 is the incompatibility of name of the sessions.

For example:

#view_pai
@yield('content')


 #view_filho
   @extend('view_pai')
   @section('content_')
   {{-- Errei o nome de propósito pra demonstrar --}}
   <div>Alguma coisa</div>
   @stop

If you do View::make('view_filho'), nothing will be returned, since you have not defined the section with the correct value.

  • I put the Return as you suggested and returned "Something wrong". Regardless of whether Return is inside or outside the if is working when I return a string. But it does not return to View neither inside nor outside the if :(

  • And I’m taking care that if always be satisfied

  • @Amandalima, then check if in your "Eternal time" there is a @yield('conteudo')

  • 2

    That was exactly the problem! The Yield in the template was @yield('content')

  • 1

    You know, it could only be one thing or the other. I’ll add in the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.