Question

I have just recently started working with Laravel. Great framework so far! However I have a question.

I am using a layout template like this: public $layout = 'layouts.private';

This is set in my Base_Controller:

public function __construct(){

    //Styles
    Asset::add('reset', 'css/reset.css');
    Asset::add('main', 'css/main.css');

    //Scripts
    Asset::add('jQuery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js');

    //Switch layout template according to the users auth credentials.
    if (Auth::check()) {
        $this -> layout = 'layouts.private';
    } else {
        $this -> layout = 'layouts.public';
    }

    parent::__construct();

}

However I get an error exception now when I try to access functions in my diffrent controllers, which should not call any view, i.e. when a user is going to login:

class Login_Controller extends Base_Controller {

public $restful = true;

public function post_index()
{

    $user = new User();
    $credentials = array('username' => Input::get('email'), 'password' => Input::get('password'));

    if (Auth::attempt($credentials))
    {

    } else {

    }

}

}

The error I get, is that I do not set the content of the different variables in my public $layout. But since no view is needed in this function, how do I tell Laravel not to include the layout in this function?

The best solution that I my self have come a cross (don't know if this is a bad way?) is to unset($this -> layout); from function post_index()...

To sum up my question: how do I tell Laravel not to include public $layout in certain functions, where a view is not needed?

Thanks in advance, fischer

Was it helpful?

Solution

You need to set $this->layout = null on any function you don't want to render the view.

OTHER TIPS

If a view isn't needed then it should be a redirect. What else is going on in that login method of yours?

What you should be doing is showing a login form on the GET login page. This page posts to the POST login page where you do validation and authentication. Regardless of what happens at the authentication level the user should then be redirect back to a GET request where another view will be displayed. This will either be the login form again if they failed or their control panel/home page.

This is a web development pattern called Post/Redirect/Get and should be applied in most cases. I can't think of a case where you would apply it.

In this method of yours, no view is needed, but you should still be redirecting as such.

return Redirect::to('wherever');

Remember that you must return the redirect or Laravel will assume you want to use your layout as the response.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top