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

07. Laravel 4 URL 生成器

2013-12-21 08:44 351 查看

获取当前请求 URL

// app/routes.php
Route::get('/current/url', function()
{
return URL::current();
});

将输出:
http://myapp.dev/current/url


获取当前请求 URL 并包含请求参数

// app/routes.php
Route::get('/current/url', function()
{
return URL::full();
});

将输出:
http://myapp.dev/current/url?foo=bar


获取上一个请求的 URL

// app/routes.php
Route::get('first', function()
{
// Redirect to the second route.
return Redirect::to('second');
});
Route::get('second', function()
{
return URL::previous();
});

将输出:
http://demo.dev/first


生成框架相关的 URL

指向普通路由

// app/routes.php
Route::get('example', function()
{
return URL::to('another/route', array('foo', 'bar'));
});

将输出:
http://myapp.dev/another/route/foo/bar


指向安全链接

以下两种方法皆可

return URL::to('another/route', array('foo', 'bar'), true);


return URL::secure('another/route', array('foo', 'bar'));


指向命名路由

// app/routes.php
Route::get('the/{first}/avenger/{second}', array(
'as' => 'ironman',
function($first, $second)
{
return "Tony Stark, the {$first} avenger {$second}.";
}
));
Route::get('example', function()
{
return URL::route('ironman', array('best', 'ever'));
});

将输出:
http://myapp.dev/the/best/avenger/ever


指向控制器的某个方法

// app/routes.php
Route::get('example', function()
{
return URL::action('Stark@tony', array('narcissist'));
});

生成资源相关的 URL

// app/routes.php
Route::get('example', function()
{
return URL::asset('img/logo.png');
});

将输出:
http://myapp.dev/img/logo.png


注意: 资源根目录默认是在网站的
public
目录,与人口文件同级。

当然,也支持安全链接:

以下两种方法皆可

return URL::asset('img/logo.png', true);


return URL::secureAsset('img/logo.png');


相关的快捷函数

<!-- app/views/example.blade.php -->
<a href="{{ url('my/route', array('foo', 'bar'), true) }}">My Route</a>
<a href="{{ secure_url('my/route', array('foo', 'bar')) }}">My Route</a>
<a href="{{ route('myroute') }}">My Route</a>
<a href="{{ action('MyController@myAction') }}">My Link</a>
<a href="{{ action('MyController@myAction', array('foo'), true) }}">My Link</a>
<img src="{{ asset('img/logo.png') }}" />
<img src="{{ secure_asset('img/logo.png') }}" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  laravel url