Laravel: URL::route() print only the route link without the website name

女生的网名这么多〃 提交于 2020-01-15 11:25:13

问题


I have this route

Route::get('/dashboard', array(
            'as' => 'dashboard-get',
            'uses' => 'AppController@getDashboard'
));

In the View if i write

<a href="{{ URL::route('dashboard-get') }}">Dashboard</a>

It will return me the entire link.

http://192.168.0.1/dashboard

How can get the route by name in the VIEW and only print the

/dashboard

Without the http://192.168.0.1/ part of the link


回答1:


From the code source, route method generate an absolute URL by default, you may set it to false:

<a href="{{ URL::route('dashboard-get',array(),false) }}">Dashboard</a>

Update

You can also define your own custom links by

HTML::macro('Rlinks',function($routeName,$parameters = array(),$name){ 
    return "<a href=".substr(URL::route($routeName,$parameters,false), 1) .">"
       .$name.
    "</a>";
});

Then call your macro

{{ HTML::Rlinks('dashboard-get',array(),'Dashboard') }}



回答2:


You may try something like this:

app('router')->getRoutes()->getByName('dashboard-get')->uri();


来源:https://stackoverflow.com/questions/26205114/laravel-urlroute-print-only-the-route-link-without-the-website-name

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