Im going to share how to make a dynamic Controller layout in Laravel 4. This post is for those who might prefer Controller Layout templating instead of Blade.
1. Open your BaseController.php ( inside Controllers folder) and add a property into it
class BaseController extends Controller {
/**
* Setup the layout used by the controller.
*
* @return void
*/
/*Set a layout properties here, so you can globally
call it in all of your Controllers*/
protected $layout = 'layouts.default';
protected function setupLayout()
{
if ( ! is_null($this->layout))
{
$this->layout = View::make($this->layout);
}
}
}
the layouts.default refers to your View files. if you haven't created it, then go to Views, create new folder and files 'layouts/default.php' for example.
in that file (default.php) you can write your own layout, lets pretend you wrote :
2. Now you can call your layout inside your Controller like this
class HomeController extends BaseController {
public function showHome()
{
/*now you can control your Layout it here */
$this->layout->title= "Hi I am a title"; //add a dynamic title
$this->layout->content = View::make('home');
}
}
3. If you're going to make the content to be dynamic and you put it inside your default layout.
Now lets go to Views and create a file named 'myContent.php'. write codes inside it
/*This how you Start a Section in a view*/
View::startSection('content'); ?>
Halooo, im a dynamic content
Now go back to your default layout ( default.php) and edit the codes :
Now you can Call it inside your Controller :
..................................
public function showContent()
{
$this->layout->title= "Hi I am a content";
$this->layout->content = View::make('myContent');
}
...............................
And there you Go. Your Dynamic Layout ( non-blade templating) is ready.
Thank You.
Reference : http://laravel.com/docs/templates