您的位置:首页 > 编程语言 > PHP开发

[转]Laravel 4之路由

2014-02-22 14:01 423 查看
Laravel 4之路由
http://dingjiannan.com/2013/laravel-routing/

Laravel 4路由是一种支持RESTful的路由体系, 基于symfony2的Routing组件构成,语法简洁明了,功能强大。关于RESTful,参考理解RESTful架构这篇文章。Laravel应用中的大多数路都会定义在
app/routes.php
文件中。

基本路由

最基本的Laravel路由由URI和闭包回调函数(匿名函数)组成。第二个参数可以是一个匿名函数,也可以是一个数组,用于指定过滤器或是HTTPS协议等

Route::get('my/page',function(){


return'Hello world!';


});


当URL以GET方式访问
http://localhost/my/page
时,将返回Hello world!字样。Route支持以下方法捕捉不同的HTTP动作

Route::get();


Route::post();


Route::put();


Route::delete();


Route::any();


带参数的路由

带参数的路由可以很容易将一个值通过URL传递给程序,比如

Route::get('/books/{genre}',function($genre)


{


return"Books in the {$genre} category.";


});


可选路由参数

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
中定义

// 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。

点击查看评论
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: