.net轉php laraval架構學習系列(三)項目實戰---Route&Controllers

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.