php 生成相对于laravel 4 中的基本网址的网址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16790086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:42:19  来源:igfitidea点击:

Generating url relative to the base url in laravel 4

phpurllaravellaravel-4

提问by Suresh

I'm new to Laravel & right now building one application on L-4 but got stuck at one place. Can't able to understand how to generate url relative to base url. In laravel-3 i know this can be done by

我是 Laravel 的新手,现在正在 L-4 上构建一个应用程序,但卡在了一个地方。无法理解如何生成相对于基本 url 的 url。在 laravel-3 我知道这可以通过

$url = URL::to('user/profile'); 

But, in L-4 how we can do this.. ?

但是,在 L-4 中我们如何做到这一点..?

回答by Holger Weis

To generate a relativeURL, you can use URL::routeor URL::actionas they allow to pass a $absoluteparameter which defaults to true. So to get a relative URL when using named routes for example, you can use the following:

要生成相对URL,您可以使用URL::routeURL::action因为它们允许传递$absolute默认为true. 因此,例如,要在使用命名路由时获取相对 URL,您可以使用以下命令:

URL::route('foobar', array(), false)

This will generate a URL like /foobar.

这将生成一个像/foobar.

回答by deepika jain

First you need to create a Named Route like

首先,您需要创建一个命名路由,例如

Say yo want to go to http://baseurl/userand runs the method 'showuser' define in controller 'allusers'

假设你想去http://baseurl/user并运行控制器“allusers”中定义的方法“showuser”

then your Route shold look like this:-

那么你的 Route shold 看起来像这样:-

Route::get('user', array('as' => 'myuser', 'uses' => 'allusers@showuser'));

Now your URL to /user would be

现在你到 /user 的 URL 将是

$myuserurl = URL::to('/myuser');
echo $myuserurl; // would be http://baseurl/user

I hope this helps you. Pls refer http://laravel.com/docs/routing#named-routes

我希望这可以帮助你。请参考http://laravel.com/docs/routing#named-routes