Generating url relative to the base url in laravel 4

[亡魂溺海] 提交于 2019-12-12 08:36:38

问题


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

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

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


回答1:


To generate a relative URL, you can use URL::route or URL::action as they allow to pass a $absolute parameter which defaults to true. So to get a relative URL when using named routes for example, you can use the following:

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

This will generate a URL like /foobar.




回答2:


First you need to create a Named Route like

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

then your Route shold look like this:-

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

Now your URL to /user would be

$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



来源:https://stackoverflow.com/questions/16790086/generating-url-relative-to-the-base-url-in-laravel-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!