Laravel default Auth :: routes () inside group prefix

I am trying to create a prefix with a variable for "companies" to enter the platform. All users are tied to the company, so the usual /login not needed. I would like to use something like `/ acme-company-name / login

I use Laravel auth by default via: php artisan make:auth on a new installation.

 Route::group(['prefix' => '{company}'], function () { Auth::routes(); }); 

When I try to switch to /company-name/login , I see the following error:

Missing required parameters for [Route: login] [URI: {company}/login].

Inside the automatically generated login.blade.php I see a call to the route('login') function, and it looks like where everything breaks down. I think I need to somehow provide a variable for this function, or redefine which โ€œentryโ€ route at some point? I would prefer not to replace the call to Auth::routes() , but I will definitely do it if necessary to fix this problem.

I should note that I tried to define the group 'as' => 'company' and change the route('company.login') , but then I was told that the company.login route was not defined.

+5
source share
5 answers

Can you try passing the $ company variable to a function?

 Route::group(['prefix' => '{company}'], function ($company) { Auth::routes(); }); 

And make sure you pass the company-name when calling the route as the required parameter.

In login.blade.php use {{ url("$company/login") }} instead of route('login') .

0
source

route() helper has more than one parameter;)

 route('login', ['company' => $company]) 
0
source

you need to share your prefix in your views and set the route as follows:

 Route::group(['prefix' => '{company}'], function ($company) { view()->share('company', $company); // share $company in views Auth::routes(); }); 

now you have $company , which is an instance of Router , and you need access to the route prefix value, for this you need to get the current route and get the company parameter so that you rewrite your helper function route() as follows:

  {{ route('login',['company'=>$company->getCurrentRoute()->__get('company')]) }} // getCurrentRoute Get the currently dispatched route instance //__get Dynamically access route parameters 

EDIT:

you can write a custom helper function as follows:

  /** * Generate the URL to a named route for Company Auth. * * @param string $name * @param Router $company * @return string */ function companyRoute($name,$company) { return app('url')->route($name, ['company'=>$company->getCurrentRoute()->__get('company')] ,true); } 
0
source

Please check the code that works great for me.

 Route::group(['prefix' => '/{company}'], function () { // ensure that auth controllers exists in right place (here it is App\Http\Controllers\Auth\LoginController) // in LoginController funtion Auth::routes(); //or you can try using custom routing like this Route::get('{something}', function($company, $something) { var_dump($company, $something); }); }); 

If you define this route at the end of the file, then the error that you spoke about, we encounter.

 ErrorException in UrlGenerationException.php line 17: Missing required parameters for [Route: login] [URI: {company}/login]. (View: \resources\views\auth\login.blade.php) 

Try defining this route as the first route in web.php and check.

thanks

0
source

Inside the login.blade.php autogenerator, I see a function call route ("login"), and this seems to happen where everything breaks down.

Yes. Performing

 Route::group(['prefix' => '{company}'], function () { Auth::routes(); }); 

you do all auth routes with the company parameter. So, in your views, instead of route('login') you can do route('login', ['company' => Request::route('company')]) . (And the same for all auth routes).

Then, probably in your App\Http\Controllers\Auth\LoginController you need to override the login method:

 public function login(Request $request, $company) { // you can copy some behaviour from // https://github.com/laravel/framework/blob/5.4/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php#L28 } 
0
source

Source: https://habr.com/ru/post/1267198/


All Articles