Justin's Words

Laravel 笔记(二)

本篇讲路由 (Routes)

查看官方文档

控制路由的文件为 app/routes.php

我的 app/routes.php 一览:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/*
|------------------------------------------
| Route Pattern
|------------------------------------------
|
*/
Route::pattern('identify', '(undergraduate|pro)');
Route::pattern('id', '[1-9]+');

/*
|------------------------------------------
| Route to Home
|------------------------------------------
|
*/
Route::get('/', ['as' => 'home', 'uses' => 'HomeController@getHome']);
Route::get('home', ['as' => 'home', 'uses' => 'HomeController@getHome']);

/*
|------------------------------------------
| Route to Tutors
|------------------------------------------
|
*/
Route::get('tutors', ['as' => 'tutors', 'uses' => 'HomeController@getTutors']);
Route::get('tutors/identify/{identify}', 'HomeController@getTutorsIdentify');
Route::get('tutors/detail/{id}/info', 'HomeController@getTutorsDetail');
Route::get('tutors/s', 'HomeController@searchTutors');

/*
|------------------------------------------
| Route to Charges
|------------------------------------------
|
*/
Route::get('charge', ['as' => 'charge', 'uses' => 'HomeController@getCharge']);

/*
|------------------------------------------
| Route to News
|------------------------------------------
|
*/
Route::get('news', ['as' => 'newsAchieve', 'uses' => 'HomeController@getNewsArchieve']);
Route::get('news/{id}', ['as' => 'newsDetail', 'uses' => 'HomeController@getNewsDetails']);

/*
|------------------------------------------
| Route to User Login & Logout
|------------------------------------------
|
*/
Route::get('login', ['as' => 'userLogin', 'uses' => 'LoginRegisterController@getUserLogin']);
Route::post('login', ['as' => 'userLogin', 'uses' => 'LoginRegisterController@postUserLogin']);
Route::get('logout', ['as' => 'userLogout', 'uses' => 'LoginRegisterController@userLogout']);

/*
|------------------------------------------
| Route to User Register
|------------------------------------------
|
*/
Route::get('register', ['as' => 'userRegister', 'uses' => 'LoginRegisterController@getUserAgreement']);
Route::get('register/agreement', ['as' => 'userRegisterAgreement', 'uses' => 'LoginRegisterController@getUserAgreement']);
Route::get('register/info', ['as' => 'userRegisterInfo', 'uses' => 'LoginRegisterController@getUserRegister']);
Route::any('register/success', ['as' => 'userRegisterSuccess', 'uses' => 'LoginRegisterController@postUserRegister']);

/*
|------------------------------------------
| Route to Dashboard
|------------------------------------------
|
*/
Route::resource('dashboard', 'UserController', array('only' => array('index', 'edit', 'update', 'destroy')));
Route::get('dashboard/{id}/edit/password', ['as' => 'userPasswordChange', 'uses' => 'UserController@getPasswordChange']);
Route::post('dashboard/{id}/edit/password', ['as' => 'userPasswordChange', 'uses' => 'UserController@postPasswordChange']);

/*
|------------------------------------------
| Route to Admin Login & Logout
|------------------------------------------
|
*/
Route::get('admin/login', ['as' => 'adminLogin', 'uses' => 'LoginRegisterController@getAdminLogin']);
Route::post('admin/login/ajax', ['as' => 'adminLoginAjax', 'uses' => 'LoginRegisterController@postAdminLogin']);
Route::get('admin/logout', ['as' => 'adminLogout', 'uses' => 'LoginRegisterController@adminLogout']);

/*
|------------------------------------------
| Route to Admin
|------------------------------------------
|
*/
Route::resource('admin/news', 'NewsController');
Route::resource('admin', 'AdminController');

接下来一个一个开始说。

Route::pattern()

Route::pattern() 是为路由里面的参数准备的,接受两个参数,第一个为变量,第二个为正则表达式,比如下面这个:

1
2
Route::pattern('id', '[1-9]+');
Route::get('tutors/detail/{id}/info', 'HomeController@getTutorsDetail');

这就限制了路由参数 id 只能为非零数字,并且不能为空,这样 http://example.com/tutors/detail/1/info 就可以访问,而 http://example.com/tutors/detail/a/info 就不能访问,404

Route::get()

Route::get() 响应 GET 请求,第一个参数为路由。

直接使用回调:

1
2
3
Route::get('home', function() {
return 'Hello world';
});

调用指定模板:

1
2
3
Route::get('home', function() {
return View::make('pages.home');
});

过滤,过滤的文档是 app/filters.php,默认有 auth 等,autu 用于过滤未登陆请求:

1
2
3
Route::get('home', array('before' => 'auth', function() {
return View::make('pages.dashboard.index');
}));

filters.php 部分代码如下(我有修改过以符合多 Auth 管理,不明白可以看你自己的 filters.php):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
App::before(function($request)
{
//
});

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

Route::filter('auth', function()
{
// for multi model supply
$query = Request::segment(1);
$model = ($query == 'dashboard') ? 'user' : 'admin';
// 如果访问状态为未登录
if (Auth::$model()->guest())
{
// 如果为 Ajax 请求,返回 401 错误
if (Request::ajax())
{
return Response::make('Unauthorized', 401);
}
// 否则跳转到登陆页面
else
{
return Redirect::guest(($model == 'user') ? 'login' : 'admin/login');
}
}
});

指定路由使用控制器,同时用 as 给控制器命名方便以后调用:

1
Route::get('/', ['as' => 'home', 'uses' => 'HomeController@getHome']);

Route::any()

响应包括 GETPOST 的请求。

Route::post()

响应 POST 请求,提交表单时用到,用法和 Route::get() 差不多。

Route::resource()

CRUD 而生,只需要指定控制器即可,该控制器建议 php artisan controller:make ControllarName 生成,因为里面的方法正是对应 CRUD 的,第三个参数可以指定只使用那些方法。

1
Route::resource('dashboard', 'UserController', array('only' => array('index', 'edit', 'update', 'destroy')));

第二部分完。