首页 > 代码库 > .net转php laraval框架学习系列(三)项目实战---Route&Controllers

.net转php laraval框架学习系列(三)项目实战---Route&Controllers

本章来学习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‘ => ‘UserController@showProfile‘));//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‘ => ‘PostController@showPost‘]);Route::post(‘/post/{post}/comment‘, [‘as‘ => ‘comment.new‘, ‘uses‘ => ‘CommentController@newComment‘]);/* 管理群组路由需要认证 */Route::group([‘prefix‘ => ‘admin‘, ‘before‘ => ‘auth‘], function () {    /*get routes    Route::get(‘/‘, [‘use‘=>‘PostController@showIndex‘]);    */    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‘ => ‘PostController@listPost‘]);    Route::get(‘/post/new‘, [‘as‘ => ‘post.new‘, ‘uses‘ => ‘PostController@newPost‘]);    /* 模型绑定 */    Route::get(‘/post/{post}/edit‘, [‘as‘ => ‘post.edit‘, ‘uses‘ => ‘PostController@editPost‘]);    Route::get(‘/post/{post}/delete‘, [‘as‘ => ‘post.delete‘, ‘uses‘ => ‘PostController@deletePost‘]);    Route::get(‘/comment/list‘, [‘as‘ => ‘comment.list‘, ‘uses‘ => ‘CommentController@listComment‘]);    /* 模型绑定 */    Route::get(‘/comment/{comment}/show‘, [‘as‘ => ‘comment.show‘, ‘uses‘ => ‘CommentController@showComment‘]);    Route::get(‘/comment/{comment}/delete‘, [‘as‘ => ‘comment.delete‘, ‘uses‘ => ‘CommentController@deleteComment‘]);    /*post routes*/    Route::post(‘/post/save‘, [‘as‘ => ‘post.save‘, ‘uses‘ => ‘PostController@savePost‘]);    Route::post(‘/post/{post}/update‘, [‘as‘ => ‘post.update‘, ‘uses‘ => ‘PostController@updatePost‘]);    Route::post(‘/comment/{comment}/update‘, [‘as‘ => ‘comment.update‘, ‘uses‘ => ‘CommentController@updateComment‘]);});/* 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