ajreal
ajreal

Reputation: 47321

laravel 4 - How to load different layout for a particular method?

I have something like this :

class AController extends BaseController
{
  protected $layout = "templates.layouts.master";
  protected $data = "something";

  public function alt()
  {
    // this is wrong
    // i want to override "templates.layouts.master"
    // missing something obviously here
    $this->layout = ??? what should do?
    $this->layout->content = View::make("content", $this->data);
  }
}

In method alt, I wish to use different layout than the default "templates.layouts.master".

I have very limited laravel 4 knowledge. This maybe something easy to achieve, but is beyond my knowledge.

Possible solutions that I forsee:

  1. define a construct method, and detect what is the current method, and set a different value for $layout (However, I not sure how to get the current method name).
  2. do an assignment like what I put in above.

Which is the correct way?

Upvotes: 3

Views: 3067

Answers (2)

fideloper
fideloper

Reputation: 12293

You can set the layout to be another view per method:

class AController extends BaseController
{
    protected $layout = "templates.layouts.master";
    protected $data = "something";

    public function alt()
    {
        $this->layout = View::make('templates.layouts.alt');
        $this->layout->content = View::make("content", $this->data);
    }
}

If you check out the BaseController, you'll see that all it does is call View::make() to set the layout view. You can do the same to over-ride its default.

Upvotes: 8

ajreal
ajreal

Reputation: 47321

OK, solution 1 seems to be possible, but I think is fugly :

class AController extends BaseController
{
  public function __construct()
  {
    if (Request::is("..."))
    {
        $this->layout = "alternative layout";
    }
  }
}

Upvotes: 1

Related Questions