Laravel - 为了获得当前前缀{{URL :: to(' prefix / search')}}而编写的内容

时间:2016-05-31 06:27:47

标签: php laravel url laravel-5 routing

基本上我要做的是自动更改de前缀,所以我只有一个视图。链接可能看起来像这样。

{{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", "CalifornaPositionController@all");
    Route::get('/search',['uses' => 'CalifornaPositionController@getSearch','as' => 'search']);
    Route::get('/show/{id}', 'CalifornaPositionController@show');

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

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

});

2 个答案:

答案 0 :(得分:2)

根据Laravel API文档RequestRoute下面的代码检索当前路由前缀

Request::route()->getPrefix()

答案 1 :(得分:1)

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

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

如果这更有意义。

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