Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel route prefix ignore in resource method parameters

I need to implement localization in my project. I added a group with a prefix.

Route::group([
    'prefix' => '{locale}',
    'where' => ['locale' => '[a-zA-Z]{2}'],
    'middleware' => 'setlocale'
], function () {

   ...

   Route::resource('projects', 'ProjectsController');

   ...

})

Middleware setlocale

<?php

namespace App\Http\Middleware;

use Closure;

class SetLocale
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        app()->setLocale($request->segment(1));
        return $next($request);
    }
}

Routers list

+--------+----------------------------------------+--------------------------------------------+---------------------------+----------------------------------------------------------------------------+---------------------+
| Domain | Method                                 | URI                                        | Name                      | Action                                                                     | Middleware          |
+--------+----------------------------------------+--------------------------------------------+---------------------------+----------------------------------------------------------------------------+---------------------+
|        | POST                                   | {locale}/projects                          | projects.store            | App\Http\Controllers\ProjectsController@store                              | web,setlocale,auth  |
|        | GET|HEAD                               | {locale}/projects                          | projects.index            | App\Http\Controllers\ProjectsController@index                              | web,setlocale,auth  |
|        | GET|HEAD                               | {locale}/projects/create                   | projects.create           | App\Http\Controllers\ProjectsController@create                             | web,setlocale,auth  |
|        | DELETE                                 | {locale}/projects/{project}                | projects.destroy          | App\Http\Controllers\ProjectsController@destroy                            | web,setlocale,auth  |
|        | PUT|PATCH                              | {locale}/projects/{project}                | projects.update           | App\Http\Controllers\ProjectsController@update                             | web,setlocale,auth  |
|        | GET|HEAD                               | {locale}/projects/{project}                | projects.show             | App\Http\Controllers\ProjectsController@show                               | web,setlocale,auth  |
|        | GET|HEAD                               | {locale}/projects/{project}/edit           | projects.edit             | App\Http\Controllers\ProjectsController@edit                               | web,setlocale,auth  |
+--------+----------------------------------------+--------------------------------------------+---------------------------+----------------------------------------------------------------------------+---------------------+

Everything works perfectly. But there is one problem. When i open route:

http://127.0.0.1:8000/en/projects/1 (Show project)

I get an error

Argument 1 passed to App\Http\Controllers\ProjectsController::show() must be an instance of App\Project, string given

I add $locale to my show method, and it works

public function show($locale, Project $project, Request $request)
{
}

How can I get rid of a parameter in a method so as not to insert it everywhere.

I tried this method and it does not work:

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function callAction($method, $parameters)
    {
        unset($parameters['locale']);
        return parent::callAction($method, $parameters); // TODO: Change the autogenerated stub
    }
}

Error

Argument 1 passed to App\Http\Controllers\ProjectsController::show() must be an instance of App\Project, instance of Illuminate\Http\Request given

like image 432
Evgeniy Mikhalichenko Avatar asked Jan 01 '23 18:01

Evgeniy Mikhalichenko


1 Answers

Add this in your middleware after setting locale:

$request->route()->forgetParameter('locale');

You can now be able to remove $locale from the show() method or any other method that uses it.

like image 181
Zeshan Avatar answered Jan 05 '23 06:01

Zeshan