I am trying to implement a simple localization for an existing Laravel project.
Implementing Localization based on the following tutorial:
https://laraveldaily.com/multi-language-routes-and-locales-with-auth/
Here is the simplified code before localization implementation:
web.php
Route::get('/poll/{poll_id}', 'AppHttpControllersPollsController@view');
PollsController@view
public function view($poll_id){
echo "poll_id: ".$poll_id;
}
TEST
URL: http://domain.name/poll/1
RESULT: poll_id: 1
Here are the simplified changes required for localization and the result I get:
web.php
Route::group(['prefix' => '{locale}', 'where' => ['locale' => '[a-zA-Z]{2}'], 'middleware' => 'setlocale'], function() {
Route::get('/poll/{poll_id}', 'AppHttpControllersPollsController@view');
});
Middleware/SetLocale
namespace AppHttpMiddleware;
use Closure;
use IlluminateHttpRequest;
class SetLocale
{
/**
* Handle an incoming request.
*
* @param IlluminateHttpRequest $request
* @param Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next){
app()->setLocale($request->segment(1));
return $next($request);
}
}
PollsController@view remained unchanged.
Now, when I open the following URL (http://domain.name/en/poll/1), the result is:
RESULT: poll_id: en
QUESTION
Is there a way to ignore "'prefix' => '{locale}'" in controller or get arguments somehow shifted so that in the controller I still get poll_id=1, not locale=en?
PS. The easiest fix would be to add another argument to PollsController@view in the following way, but it does not smell well and then I would need to add locale argument to all functions, although I do not use it there:
public function view($locale, $poll_id){
echo "poll_id: ".$poll_id;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…