laravel url管理与使用
2014-09-13 20:59 youxin 阅读(4599) 评论(0) 编辑 收藏 举报
获取当前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.
return Redirect::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>
结束
关于url可以看官网的api
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通