This chapter to learn the route of Laravel
A simple routing sample
Route::get ('/', function () { return ' Hello World ';});
Routing is similar to the style of node. The above route directly returns Contentresult so easy to understand some.
And look at a complicated route.
Route::filter (' old ', function ()//This is a filter{ if (input::get (' age ') <) { return redirect::to (' Home ');
Array
' as ' = ' profile ',//Routing name
' Before ' = ' auth|old ',//Multiple filter
' Uses ' = ' [email protected] ');//controller and action
For more information http://v4.golaravel.com/docs/4.1/routing
Next is the controller in the Controller,laravel framework under the App/controllers folder.
All controllers are inherited Basecontroller.
There are many similarities between Laravel's controller and ASP.
Let's say Controller Filters. The above route is already in use.
Or look at the code, the complete route.php code
<?php/* Model Binding */route::model (' post ', ' post '); Route::model (' comment ', ' comment ');/* Routes that anonymous users can access */route::get ('/post/{post}/show ', [' as ' = ' post.show ', ' uses ' = > ' [email protected] '); Route::p Ost ('/post/{post}/comment ', [' as ' = ' comment.new ', ' uses ' = ' [email protected] ');/* Manage group routing requires authentication * /route::group ([' prefix ' = ' admin ', ' before ' = ' auth '], function () {/*get routes route::get ('/', [' Use ' => ;‘ [email protected]]); */Route::get ('/', function () {$layout = View::make (' shared.layout '); $layout->title = ' DashBoard '; $username = Auth::user ()->username; $layout->main = View::make (' Post.index '); return $layout; }); Route::get ('/post/list ', [' as ' = ' post.list ', ' uses ' = ' [email protected] ']); Route::get ('/post/new ', [' as ' = ' post.new ', ' uses ' = ' [email protected] ']); /* Model Bindings */route::get ('/post/{post}/edit ', [' as ' = ' post.edit ', ' uses ' = ' = ') [EMAIL&NBsp;protected]); Route::get ('/post/{post}/delete ', [' as ' = ' post.delete ', ' uses ' = ' [email protected] ']); Route::get ('/comment/list ', [' as ' = ' comment.list ', ' uses ' = ' [email protected] ']); /* Model Bindings */route::get ('/comment/{comment}/show ', [' as ' = ' comment.show ', ' uses ' = ' [email protected] ']); Route::get ('/comment/{comment}/delete ', [' as ' = ' comment.delete ', ' uses ' = ' [email protected] ']); /*post routes*/Route::p ost ('/post/save ', [' as ' = ' post.save ', ' uses ' = ' [email protected] ']); Route::p ost ('/post/{post}/update ', [' as ' = ' post.update ', ' uses ' = ' [email protected] ']); Route::p ost ('/comment/{comment}/update ', [' as ' = ' comment.update ', ' uses ' = ' [email protected] ']); * RESTful Controllers Reason processing a series of routes is somewhat similar to the */route::controller ('/', ' BlogController ') of the ASP./* View synthesizer */view:: Composer (' Shared.sidebar ', function ($view) {$view->recentposts = Post::orderbY (' id ', ' desc ')->take (5)->get ();});
Analysis from Route::controller's blogcontroller:
<?phpclass BlogController extends basecontroller{public function __construct () {//updated:prevents re-l Ogin. $this->beforefilter (' guest ', [' only ' = ' getlogin ']);//The Visitor's permission is commented out, there is no same as $this->beforefilter (' auth ', [' only ' = [' getlogout ']];//Set only logout requires authentication permission} public Function GetIndex () {$posts = Post::orderb Y (' id ', ' desc ')->paginate (10); $this->layout->title = ' Home Page | Laravel 4 Blog ';
Beginners here may be a bit difficult to understand, here and Laravel view related
The layout template is specified in Basecontroller, and the layout page has a child view of main, which merges the template of the blog folder home with the index templates and passes in the parameters $posts
As for the compact method, you can view PHP-related documentation. $this->layout->main = View::make (' blog.home ')->nest (' content ', ' Blog.index ', compact (' posts ')); } public Function GetLogin () {$this->layout->title = ' login '; $this->layout->main = View::make (' Blog.login '); The Public Function Postlogin () {$credentials = [' username ' = ' input::get (' username '), ' Password ' = input::get (' password ')]; $rules = [' username ' = ' required ', ' password ' = ' required ']; $validator = Validator::make ($credentials, $rules); if ($validator->passes ()) {if (Auth::attempt ($credentials)) return redirect::to (' admin/'); Return Redirect::back ()->withinput ()->with (' failure ', ' username or password is invalid! '); } else {return redirect::back ()->witherrors ($validator)->withinput (); }} public function getLogout () {auth::logout (); return redirect::to ('/'); }}
This chapter if the detailed writing needs to write too much, the code is not pasted out, the next chapter I put the full code out
. NET to PHP laraval Framework Learning Series (iii) Project Combat---route&controllers