標籤:des blog http io os ar for sp 檔案
本章來學習laravel的路由
一個簡單的路由列子
Route::get(‘/‘, function(){ return ‘Hello World‘;});
路由的寫法和Node的風格很相似。上面的路由直接返回ContentResult 這樣容易理解一些。
再看一個複雜的Route的
Route::filter(‘old‘, function()//這是一個filter{ if (Input::get(‘age‘) < 200) { return Redirect::to(‘home‘); }});Route::get(‘user/profile‘,
array(
‘as‘ => ‘profile‘,//路由名字
‘before‘ => ‘auth|old‘,//多個filter
‘uses‘ => ‘[email protected]‘));//controller 和action
更多理由資訊 http://v4.golaravel.com/docs/4.1/routing
接下來是Controller,Laravel架構裡的controller在App/controllers檔案夾下面。
所有的Controller都是繼承basecontroller。
laravel的控制器和asp.net mvc也有很多類似的地方
比方說 Controller Filters.上面的路由已經用到了。
還是看代碼,完整的route.php代碼
<?php/* 模型繫結 */Route::model(‘post‘, ‘Post‘);Route::model(‘comment‘, ‘Comment‘);/* 匿名使用者可以訪問的路由 */Route::get(‘/post/{post}/show‘, [‘as‘ => ‘post.show‘, ‘uses‘ => ‘[email protected]‘]);Route::post(‘/post/{post}/comment‘, [‘as‘ => ‘comment.new‘, ‘uses‘ => ‘[email protected]‘]);/* 管理群組路由需要認證 */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]‘]); /* 模型繫結 */ Route::get(‘/post/{post}/edit‘, [‘as‘ => ‘post.edit‘, ‘uses‘ => ‘[email protected]‘]); Route::get(‘/post/{post}/delete‘, [‘as‘ => ‘post.delete‘, ‘uses‘ => ‘[email protected]‘]); Route::get(‘/comment/list‘, [‘as‘ => ‘comment.list‘, ‘uses‘ => ‘[email protected]‘]); /* 模型繫結 */ 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::post(‘/post/save‘, [‘as‘ => ‘post.save‘, ‘uses‘ => ‘[email protected]‘]); Route::post(‘/post/{post}/update‘, [‘as‘ => ‘post.update‘, ‘uses‘ => ‘[email protected]‘]); Route::post(‘/comment/{comment}/update‘, [‘as‘ => ‘comment.update‘, ‘uses‘ => ‘[email protected]‘]);});/* RESTful Controllers 理由處理一系列的路由有點類似於ASP.net MVC 的通配路由 */Route::controller(‘/‘, ‘BlogController‘);/* 視圖合成器 */View::composer(‘shared.sidebar‘, function ($view) { $view->recentPosts = Post::orderBy(‘id‘, ‘desc‘)->take(5)->get();});
從 Route::controller的 BlogController 來分析:
<?phpclass BlogController extends BaseController{ public function __construct() { //updated: prevents re-login. //$this->beforeFilter(‘guest‘, [‘only‘ => [‘getLogin‘]]);//這裡遊客的許可權注釋掉了,有和沒有是一樣的 $this->beforeFilter(‘auth‘, [‘only‘ => [‘getLogout‘]]);//設定只有logout需要有認證的許可權 } public function getIndex() { $posts = Post::orderBy(‘id‘, ‘desc‘)->paginate(10); $this->layout->title = ‘Home Page | Laravel 4 Blog‘;
//這裡初學者可能有點難理解,這裡和laravel的view 相關
//這裡layout模板在basecontroller中指定了布局頁,布局頁中有一個main的子視圖,這裡是把blog檔案夾下home的模版合并index模板並傳入參數$posts
//至於compact方法可以查看php相關文檔。 $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‘); } 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(‘/‘); }}
這一章如果詳細寫的話需要寫的內容太多了,代碼不一一貼出來了,在下一章 我把完整代碼貼出來
.net轉php laraval架構學習系列(三)項目實戰---Route&Controllers