[转]Laravel 4之URL生成
2014-02-22 14:03
302 查看
Laravel 4之URL生成
http://dingjiannan.com/2013/laravel-url/
输入
输入
生成的URL为
或是使用
使用URL参数
返回
或是
或是
使用路由别名
使用控制器
使用资源
http://dingjiannan.com/2013/laravel-url/
获取当前URL
获取当前URL有两种方式,URL::current()或
URL::full(),区别是返不返回GET参数如
Route::get('/current/url',function()
{
return URL::current();
});
输入
/current/url?foo=bar时只显示
http://myapp.dev/current/url。使用
URL::full()则显示
http://myapp.dev/current/url?foo=bar
获取之前的URL
// app/routes.php
Route::get('first',function()
{
// Redirect to the second route.
returnRedirect::to('second');
});
Route::get('second',function()
{
eturn URL::previous();
});
输入
/first,返回
http://loacahost,
URL::previous()返回的是之前到first的路由
生成URL
使用URL::to()生成URL,如
Route::get('example',function()
{
return URL::to('another/route', array('foo','bar'));
});
生成的URL为
http://myapp.dev/another/route/foo/bar,如需将HTTP协议变为HTTPS,则用
URL::to('another/route', array('foo','bar'),true);
或是使用
URL::secure('another/route', array('foo','bar'));
使用路由别名生成URL
Route::get('the/best/avenger', array('as'=>'ironman',function()
{
return'Tony Stark';
}));
Route::get('example',function()
{
return URL::route('ironman');
});
使用URL参数
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'));
});
到控制器的URL
// Route to the Stark controller.
Route::get('tony/the/{first}/genius','Stark@tony');
Route::get('example',function()
{
return URL::action('Stark@tony', array('narcissist'));
});
到资源的绝对URL
Route::get('example',function()
{
return URL::asset('img/logo.png');
});
返回
http://myapp.dev/img/logo.png,同样,使用HTTPS
return URL::asset('img/logo.png',true);
或是
return URL::secureAsset('img/logo.png');
在视图中生成URL
使用url()在视图中生成URL,方法跟参数跟以上的没什么区别,使用如下
<ahref="">My Route</a>
或是
<ahref="">My Route</a>
使用路由别名
<ahref="">My Route</a>
使用控制器
<ahref="">My Route</a>
使用资源
<ahref="">My Route</a>
<ahref="">My Route</a>
结束
点击查看评论相关文章推荐
- [转]Laravel 4之控制器
- [转]Laravel 4之路由
- [转]laravel 4之视图及Responses
- PHP的ISAPI和FastCGI比较
- php的SAPI,CLI SAPI,CGI SAPI
- Laravel 安装指南
- PHP的核心Zend Engine
- 【PHP】php 递归、效率和分析
- PHP Cookie学习
- ThinkPHP中实例化对象M()和D()的区别
- QPS TPS
- PHP扩展开发:一个简单的例子
- phpMyAdmin import.php 跨站脚本漏洞
- PHP中VC6、VC9、TS、NTS版本的区别与用法详解
- PHP开源框架之SugarCRM
- lamobo跑ltp出現了lowmemory錯誤
- php读取excel日期类型数据的例子
- PHP之負載均衡下的session共用
- PHP ZEND API整理
- Postfix实现代理Exchange邮件传输方案 推荐