Accessing Controller Layout Inside Route in Laravel 4
|Shares :

Accessing Controller Layout Inside Route in Laravel 4

If you're using Dynamic Controller layout in Laravel, you might sometimes want to directly render the Views inside Route. This is actually not a recommended way to render views inside Route, but whatever, you're the master of your own program. 

bellow is how we do it in Controller :
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');
    }

}

In Route, we need to use Closure, take a look at this example
/*-------------------------------------------------------
 * In Order to be able to access layout inside Route,
 * set this
 *------------------------------------------------------*/
View::name('layouts.default', 'default');
$layout = View::of('default');
//usage
Route::get('/privacy-policy',function() use($layout){
    $layout->title = 'Privacy Policy - Codertash.com';
    return $layout->nest('content','privacyPolicy',array('somedata'=>'somedata'));
}); //another example Route::get('main/signup',array('as'=>'main.signup',function() use ($layout){ return $layout ->nest('content', 'main.signup') ->nest('sidebar', 'main.menu'); }));

Thank you, comment below for discussion


Tags : #laravel #layout #controller #dynamic #route


SHARES :


Budy K's Avatar

Sains & Teknologi Enthusiast, Software Developer. Check my profile!
> 75% komen akan dibalas. Mohon sabar ya. Simpan halaman ini dan cek kembali lagi

Comments

* Email will not be published


    Privacy .ToS .Contact Us
    © 2017 Teknosains