Original Author's blog: ieqi.net
========================================================== ========================================================== ================================
View
Laravel3 follows the MVC mode. The view layer is responsible for displaying the data processed by the controller. The view layer code files are stored in the application/views directory and end with PHP.
Because PHP itself can be mixed with HTML, the view layer of the PHP framework can also be used as a template to some extent.
For example, we create a file application/views/home/index. php:
Hello World!
In this way, we can call view rendering when processing the returned request:
// Route callback method route: Get ('/', function () {return view: Make ('home. index');}); // The Public Function action_index () {return view: Make ('home. index ');});
Note that the parameters in the make () method above use. As a hierarchical Division without the PHP suffix, which is clearly consistent with the files in the Views folder.
We can use the following methods to check whether a view exists:
$exists = View::exists(‘home.index‘);
Sometimes, we need to set a special HTTP status code or HTTP header information. We can handle it in the following way:
$headers = array(‘foo‘ => ‘bar‘);return Response::make(‘Hello World!‘, 200, $headers);
Note that the above make method is not the make method in the View class, but the make method in the response.
Return to view and bind data:
return Response::view(‘home‘, array(‘foo‘ => ‘bar‘));
Return JSON format data:
return Response::json(array(‘name‘ => ‘Batman‘));
You can directly Use ORM to return JSON data, provided that you use ORM:
return Response::eloquent(User::find(1));
Bind Data Rendering View
Laravel3 provides multiple ways to bind data when rendering a view, providing developers with the greatest convenience (the syntax is a little more powerful ).
With binding
return View::make(‘home‘)->with(‘name‘, ‘James‘);
Then you can use the variable $ name in the view named home. The variable value is 'James'
With binding can be chained to use:
View::make(‘home‘) ->with(‘name‘, ‘James‘) ->with(‘votes‘, 25);
Bind an array:
View::make(‘home‘, array(‘name‘ => ‘James‘));
Bind attributes as view attributes:
$view->name = ‘James‘;$view->email = ‘[email protected]‘;
You can also use the class array method:
$view[‘name‘] = ‘James‘;$view[‘email‘] = ‘[email protected]‘;
There is always a suitable data binding method for you.
Name View
What is the significance of naming a view? Like named routing, adding a name abstraction layer can reduce the cost of modifying other parts when changing the View File Name.
First, register the view name and write it to routes. php:
View::name(‘layouts.default‘, ‘layout‘);
Used in this way when calling;
return View::of(‘layout‘);
Bind data:
return View::of(‘layout‘, array(‘orders‘ => $orders));
Of course, you can also use with to bind data.
return View::of(‘layout‘)->with(‘name‘, ‘Will‘);
View Synthesizer
Generally, there are many relatively fixed block areas on the webpage, displaying relatively fixed items, such as the navigation bar, such as the latest articles. If we bind the data to the front end every time we render the view, it will not only make the code bloated, but also not conducive to maintenance.
Digress:
This problem can be seen as a benchmark for observing a Web framework. What kind of convenience does the Web framework provide for us to develop Web? Beautiful file organization? Orm? MVC? Template system? These are all important, but to be honest, most of the frameworks are doing well now. It can be said that there is no difference except the structure and encoding style. The processing of the block area shows the characteristics of each framework. In yii, We have widgets, and in flask we have blueprint and content.
In laravel3, we can solve this problem through the view synthesizer. The view synthesizer needs to be written in the routes. php file;
View::composer(‘home‘, function($view){ $view->nest(‘footer‘, ‘partials.footer‘);});
Through the above Code, we embed the partials. footer sub-view into the home view.
We can also synthesize multiple views at a time.
// The synthesizer processes the home and profile viewviews: composer (Array ('home', 'profile '), function ($ view ){//});