2016-05-31 83 views
0

基本上我想做的就是自动更改de前缀,所以我只有一个视图。链接可能看起来像这样。Laravel - 写什么来获取当前前缀{{URL :: to('prefix/search')}}

{{URL::to('california/search')}} 
{{URL::to('florida/search')}} 
{{URL::to('arkansas/search')}} 

我使用Laravel 5.2

这是控制器我使用:

//Controllers for states 
Route::group(array('prefix' => 'california', "namespace" => 'Test'), function() {  
    Route::get("/all", "[email protected]"); 
    Route::get('/search',['uses' => '[email protected]','as' => 'search']); 
    Route::get('/show/{id}', '[email protected]'); 

}); 
Route::group(array('prefix' => 'florida', "namespace" => 'Test'), function() {  
    Route::get("/all", "[email protected]"); 
    Route::get('/search',['uses' => '[email protected]','as' => 'search']); 
    Route::get('/show/{id}', '[email protected]'); 

}); 
Route::group(array('prefix' => 'arkansas', "namespace" => 'Test'), function() {  
    Route::get("/all", "[email protected]"); 
    Route::get('/search',['uses' => '[email protected]','as' => 'search']); 
    Route::get('/show/{id}', '[email protected]'); 

}); 

回答

2

根据用于RequestRoute如下因素代码Laravel API文档检索当前路由前缀

Request::route()->getPrefix() 
+0

谢谢你,这是我需要的。 –

+0

该解决方案是否有效? – huuuk

1

这就是我解决问题的方法。

{{URL::to(Request::route()->getPrefix().'/search')}} 

如果这样做更有意义。

<?php $prefix = Request::route()->getPrefix(); ?> 
{{URL::to($prefix.'/search')}}