Laravel - What to write in view to get current pre

2019-09-06 10:47发布

问题:

Basically what I wan't to do is to automatically change de prefix so I only have one view. The links could look something like this.

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

I use Laravel 5.2

This is the controllers i use:

//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');

});

回答1:

According to Laravel API docs for Request and Route folowing code retrieves current route prefix

Request::route()->getPrefix()


回答2:

This is how I solved the problem.

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

If this makes more sense.

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