Laravel 4路由是一种支持RESTful的路由体系, 基于symfony2的Routing组件构成,语法简洁明了,功能强大。关于RESTful,参考理解RESTful架构这篇文章。Laravel应用中的大多数路都会定义在app/routes.php
文件中。php
基本路由
最基本的Laravel路由由URI和闭包回调函数(匿名函数)组成。第二个参数能够是一个匿名函数,也能够是一个数组,用于指定过滤器或是HTTPS协议等html
Route::get('my/page',function(){
return'Hello world!';
});
当URL以GET方式访问http://localhost/my/page
时,将返回Hello world!字样。Route支持如下方法捕捉不一样的HTTP动做laravel
Route::get();
Route::post();
Route::put();
Route::delete();
Route::any();
带参数的路由
带参数的路由能够很容易将一个值经过URL传递给程序,好比正则表达式
Route::get('/books/{genre}',function($genre)
{
return"Books in the {$genre} category.";
});
可选路由参数json
Route::get('/books/{genre?}',function($genre =null)
{
if($genre ==null)return'Books index.';
return"Books in the {$genre} category.";
});
带默认值的路由参数数组
Route::get('/books/{genre?}',function($genre ='Crime')
{
return"Books in the {$genre} category.";
});
支持HTTPS的安全路由
Route::get('secret/content', array(
'https',
function(){
return'Secret squirrel!';
}
));
带有正则表达式约束条件的路由
Route::get('save/{princess}',function($princess)
{
return"Sorry, {$princess} is in another castle. :(";
})->where('princess','[A-Za-z]+');
多个条件限定安全
Route::get('save/{princess}/{unicorn}',function($princess, $unicorn)
{
return"{$princess} loves {$unicorn}";
})->where('princess','[A-Za-z]+')
->where('unicorn','[0-9]+');
路由过滤器
过滤器能够在路由以前或以后进行相关的逻辑判断和动做,确保你有权限访问相应的资源。过滤器在app/filters.php
中定义restful
// app/filters.php
Route::filter('birthday',function()
{
if(date('d/m')=='16/08'){
return'Happy birthday';
}
});
在路由前绑定过滤器闭包
// app/routes.php
Route::get('/', array(
'before'=>'birthday',
function()
{
return'hello word!';
}
));
若是当天为16/08,那么输出'Happy birthday',不然输出'hello word!',一旦过滤器有返回响应,则中止路由。过滤器没有返回则路由继续。架构
也能够在路由后绑定过滤器
// app/routes.php
Route::get('/', array(
'after'=>'birthday',
function()
{
return'hello word!';
}
));
绑定多个过滤器
// app/routes.php
Route::get('/', array(
'before'=>'birthday|christmas',
function()
{
returnView::make('hello');
}
));
过滤器从左到右依次执行,若是有一个返回响应,则请求终止,执行返回的响应。也能够用数组的形式
// app/routes.php
Route::get('/', array(
'before'=> array('birthday','christmas'),
function()
{
returnView::make('hello');
}
));
过滤器参数
before
过滤器的function默认两个参数,after
过滤器默认为三个
// before
Route::filter('test',function($route, $request)
{
});
// after
Route::filter('test',function($route, $request, $response)
{
});
所以before
过滤器的第三个参数之后为用户自定义参数,after
第四个参数之后为用户自定义参数
// app/filters.php
Route::filter('birthday',function($route, $request, $date)
{
if(date('d/m')== $date){
return'Happy birthday';
}
});
路由中经过过滤器名后加:
号添加参数
Route::get('/', array(
'before'=>'birthday:16/08',
function()
{
return'hello word!';
}
));
多个过滤器参数
// app/filters.php
Route::filter('birthday',function($route, $request, $first, $second, $third)
{
return"{$first} - {$second} - {$third}";
});
// app/routes.php
Route::get('/', array(
'before'=>'birthday:foo,bar,baz',
function()
{
return'hello word!';
}
));
过滤器类
咱们能够用过滤器类代替闭包函数,方便之后测试,过滤器类能够在app/filters.php
中定义,也能够放在任何地方,假设咱们在/app
目录下新建一个filters
文件夹,专门用来放过滤器类,那么咱们必须先更改composer.json
文件,将新的目录添加进类自动加载'classmap'中
"autoload":{
"classmap":[
"app/commands",
"app/controllers",
"app/models",
"app/filters",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
}
而后建立过滤器类文件
<?php
// app/filters/Birthday.php
classBirthdayFilter
{
publicfunction filter($route, $request, $date='16/08')
{
if(date('d/m')== $date){
return'Happy bitrhday';
}
}
}
类名称没有特别约束,主要是实现filter()
函数,而后注册咱们的过滤器类
// app/filters.php
Route::filter('birthday','BirthdayFilter');
而后跟路由绑定
// app/routes.php
Route::get('/', array(
'before'=>'birthday',
function()
{
return'hello word';
}
));
在浏览以前,需运行composer dump-autoload
,更新自动加载文件,使其能找到咱们建立的类。
全局过滤器
app/filters.php
中有两个全局过滤器,适用于任何请求
App::before(function($request)
{
//
});
App::after(function($request, $response)
{
//
});
此外app/filters.php
文件中还定义了auth
,auth.basic
,guest
,csrf
四个默认过滤器
模式过滤器
能够针对一组路由绑定过滤器
// app/routes.php
Route::when('profile/*','birthday');
或是根据HTTP动做限定过滤器
// app/routes.php
Route::when('profile/*','birthday', array('post'));
命名路由
能够为较长较复杂的路由命名一个简单的名字,方便重定向或,生成URL
// app/routes.php
Route::get('/my/long/calendar/route', array(
'as'=>'calendar',
function(){
return route('calendar');
}
));
使用路由名称来建立URL和重定向
$url = URL::route('calendar');
$redirect =Redirect::route('calendar');
获取当前路由的别名
$current =Route::currentRouteName();
还能够为控制器指定路由名称
// app/routes.php
Route::get('/my/long/calendar/route', array(
'as'=>'calendar',
'uses'=>'CalendarController@showCalendar'
));
路由组
前面经过Route::when()
为一组相同路由绑定过滤器,若是要为多个不一样组的路由绑定,则须要路由组Route::group()
// app/routes.php
Route::group(array('before'=>'onlybrogrammers'),function()
{
// First Route
Route::get('/first',function(){
return'Dude!';
});
// Second Route
Route::get('/second',function(){
return'Duuuuude!';
});
});
路由前缀
为组路由设置前缀
// app/routes.php
Route::group(array('prefix'=>'books'),function()
{
// First Route
Route::get('/first',function(){
return'The Colour of Magic';
});
// Second Route
Route::get('/second',function(){
return'Reaper Man';
});
});
这样能够经过localhost/books/first
形式访问
子域名路由
经过子域名的不一样访问不一样的资源,好比下面地址
http://myapp.dev/my/route
http://another.myapp.dev/my/route
http://third.myapp.dev/my/route
路由以下
// app/routes.php
Route::group(array('domain'=>'myapp.dev'),function()
{
Route::get('my/route',function(){
return'Hello from myapp.dev!';
});
});
Route::group(array('domain'=>'another.myapp.dev'),function()
{
Route::get('my/route',function(){
return'Hello from another.myapp.dev!';
});
});
一样,子域名里还能够传递参数
Route::group(array('domain'=>'{user}.myapp.dev'),function()
{
Route::get('profile/{page}',function($user, $page){
// ...
});
});
结束
Laravel提供的路由功能不只这些,还包括控制器路由,路由跟模型的绑定,甚至支持建立resource controllers。