Add desmart\laravel-layout
as a requirement to composer.json:
{
"require": {
"desmart/laravel-layout": "1.2.*"
}
}
Update your packages with composer update
or install with composer install
.
In app/config/app.php add:
'DeSmart\Layout\LayoutServiceProvider',
to providers'Layout' => 'DeSmart\Layout\Facades\Layout',
to aliases.
This package provides DeSmart\Layout\Controller
class which works like normal page controller.
However it can be used to describe complete page structure.
To do this simply define $layout
which is basic page template, and $structure
which is an array with section block definition.
Section block is a list of callbacks (let's call them actions) which will be run and put in a defined place in template.
<!DOCTYPE html>
<html>
<head></head>
<body>
<div class="container">
<div class="main">{{ $main }}</div>
<div class="right">{{ $right }}</div>
</div>
</body>
</html>
<?php
class SampleController extends \DeSmart\Layout\Controller {
protected $layout = 'layouts.default';
protected $structure = array(
'main' => array(
'FancyBanner@show',
'TopStories@show',
),
'right' => array(
'Menu@showTopProducts',
),
);
public function showProducts() {
$this->structure['main'] = array(
'Products@showAll',
);
return $this->execute();
}
public function showOne() {
$this->changeLayout('layouts.product');
$this->structure['main'] = array(
'Products@showOne',
);
return $this->execute();
}
}
<?php
Route::get('/', 'SampleController@execute');
Route::get('/products', 'SampleController@showProducts');
Route::get('/products/{product_id}', 'SampleController@showOne');
Each action is a callback string which will be called during DeSmart\Layout\Controller@execute
call.
Every action can get params defined in route, just define them as function arguments (public function showOne($product_id) {}
).
This package provides Layout
facade with method dispatch
.
It can be used to execute action directly in template.
<header>{{ Layout::dispatch('HomeController@head') }}</header>
dispatch()
can take array argument with named callback arguments:
class FancyController {
public function test($name, $title = 'sir. ') {}
}
<header> {{ Layout::dispatch('FancyController@test', array('name' => 'Hans')) }} </header>
Notice, that it takes care with default arguments.
This package is provided as is. For now it's only a concept and the whole idea can change.
Just treat it as early alpha version.