用laravel4.2实现一个简单的图片墙博客(四)路由器、过滤器(Routes, filters)

关于Laravel 路由和过滤器的写法,请查看laravel手册-路由部分

上一节已经完成了控制器相关的定义,但是如果没有路由导向这些控制器类的函数,这些定义就只是空谈,接下来我们要定义路由器的功能。

laravel的路由器定义非常简洁,也非常强大,它可以直接定义一个路由的操作,也可以把所有前缀路由统一处理,最重要的功能,它可以映射路由和控制器,使得可以模块化地处理用户的请求。

1#......routes.php

我们绑定了路由picture和comment到它们所指的控制器,所有的定义几乎不用说明就可以看懂。

<?php


/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/


/* Model Bindings */
Route::model('picture', 'Picture');
Route::model('comment', 'Comment');


/* User routes */
Route::get('/picture/{picture}/show', ['as' => 'picture.show', 'uses' => 'PictureController@showPicture']);
Route::post('/picture/{picture}/comment', ['as' => 'comment.new', 'uses' => 'CommentController@newComment']);


/* Admin routes */
Route::group(['prefix' => 'admin', 'before' => 'auth'], function () {
    /*get routes*/
    Route::get('dash-board', function () {
        $layout = View::make('master');
        $layout->title = 'DashBoard';
        $layout->main = View::make('dash')->with('content', 'Hi, Welcome to Dashboard!');
        return $layout;


    });
    Route::get('/picture/list', ['as' => 'picture.list', 'uses' => 'PictureController@listPicture']);
    Route::get('/picture/new', ['as' => 'picture.new', 'uses' => 'PictureController@newPicture']);
    Route::get('/picture/{picture}/edit', ['as' => 'picture.edit', 'uses' => 'PictureController@editPicture']);
    Route::get('/picture/{picture}/delete', ['as' => 'picture.delete', 'uses' => 'PictureController@deletePicture']);
    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']);


    /*picture routes*/
    Route::post('/picture/save', ['as' => 'picture.save', 'uses' => 'PictureController@savePicture']);
    Route::post('/picture/{picture}/update', ['as' => 'picture.update', 'uses' => 'PictureController@updatePicture']);
    Route::post('/comment/{comment}/update', ['as' => 'comment.update', 'uses' => 'CommentController@updateComment']);


});


/* Home routes */
Route::controller('/', 'BlogController');


/* View Composer */
View::composer('sidebar', function ($view) {
    $view->recentPictures = Picture::orderBy('id', 'desc')->take(5)->get();
});



过滤器用于对路由的,根据信息筛选的结果选择是否执行这个路由的功能,或者执行过滤器的功能(比如判断用户登录状态),可以在request前/后执行过滤 器.Laravel自带了auth验证机制,使用before过滤可以防止未授权用户查看需要授权访问的页面,csrf是防止跨站攻击的一种过滤,使用remember_token唯一标示用户发送的数据。

2#......filters.php

<?php

/*
|--------------------------------------------------------------------------
| Application & Route Filters
| DownLoad From Http://Www.UncleToo.Com
|--------------------------------------------------------------------------
|
| Below you will find the "before" and "after" events for the application
| which may be used to do any work before or after a request into your
| application. Here you may also register your custom route filters.
|
*/

App::before(function($request)
{
	//
});


App::after(function($request, $response)
{
	//
});

/*
|--------------------------------------------------------------------------
| Authentication Filters
|--------------------------------------------------------------------------
|
| The following filters are used to verify that the user of the current
| session is logged into this application. The "basic" filter easily
| integrates HTTP Basic authentication for quick, simple checking.
|
*/

Route::filter('auth', function()
{
	if (Auth::guest()) return Redirect::guest('login');
});


Route::filter('auth.basic', function()
{
	return Auth::basic();
});

/*
|--------------------------------------------------------------------------
| Guest Filter
|--------------------------------------------------------------------------
|
| The "guest" filter is the counterpart of the authentication filters as
| it simply checks that the current user is not logged in. A redirect
| response will be issued if they are, which you may freely change.
|
*/

Route::filter('guest', function()
{
	if (Auth::check()) return Redirect::to('/');
});

/*
|--------------------------------------------------------------------------
| CSRF Protection Filter
|--------------------------------------------------------------------------
|
| The CSRF filter is responsible for protecting your application against
| cross-site request forgery attacks. If this special token in a user
| session does not match the one given in this request, we'll bail.
|
*/

Route::filter('csrf', function()
{
	if (Session::token() != Input::get('_token'))
	{
		throw new Illuminate\Session\TokenMismatchException;
	}
});

在View中,我们可以定义一向操作的链接,或者重定向,根据路由可以指向我们需要运行的控制器或者函数代码。方便我们对各个模块的管理。

你可能感兴趣的:(数据库,PHP,laravel,网页制作,laravel框架)