mercredi 24 juillet 2019

Dynamic prefix routing in laravel

I'm currently facing an issue with the prefix routing. I'm not sure if I'm doing it right for the routing, can you help me?

Here is the context: My website has 2 front and 1 back. All views will kind of be similar, just some section will disappear.

Urls should be like this:

localhost/admin
localhost/front1
localhost/front2 

When I tried to go to localhost/front1/login, I have this error

 Missing required parameters for [Route: login] [URI: {brand}/login]. (View: F:\StockageUnit2\Developpement\projets\billetterie\resources\views\auth\login.blade 

What am I doing wrong ?

And how do I differentiate which route I'm using on my template for the href ?

How I am supposed to redirect depending on the prefix as href is determined by the route name?

I read the documentation and found no clues about it...

```
**app\Providers\RouteServiceProvider.php**

public function boot() { 
    parent::boot(); 
    Route::bind('brand', function ($value) { 
        return Brand::where('slug', $value)->first() ?? abort(404); 
    }); 
} 

**routes/web.php** 

use Illuminate\Support\Facades\Auth;
use \App\Brand;

Route::get('/' , 'HomeController@index')->name('main');

/*Back*/
Route::group([
    'prefix' => 'admin'
], function () {
    Route::get('/' , 'AdminController@index')->name('admin.home');
    AuthRoutes();
});

/*Front*/
Route::group([
    'prefix' => '{brand}'
], function () {
    Route::get('/' , 'BrandsLandingController@index')->name('main_landing');
    AuthRoutes();
});

function AuthRoutes()
{
    Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
    Route::post('login', 'Auth\LoginController@login');
    Route::post('logout', 'Auth\LoginController@logout')->name('logout');
    Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
    Route::post('register', 'Auth\RegisterController@register');
    Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
    Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
    Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
    Route::post('password/reset', 'Auth\ResetPasswordController@reset')->name('password.update');
    Route::get('email/verify', 'Auth\VerificationController@show')->name('verification.notice');
    Route::get('email/verify/{id}', 'Auth\VerificationController@verify')->name('verification.verify');
    Route::get('email/resend', 'Auth\VerificationController@resend')->name('verification.resend');
}

**route:list**

|        | GET|HEAD | /                              | main                | App\Http\Controllers\HomeController@index                              | 
|        | GET|HEAD | admin                          | admin.home          | App\Http\Controllers\AdminController@index                             | |        
|        | GET|HEAD | admin/email/resend             | verification.resend | App\Http\Controllers\Auth\VerificationController@resend                | web,auth,throttle:
|        | GET|HEAD | admin/email/verify             | verification.notice | App\Http\Controllers\Auth\VerificationController@show                  | web,|
|        | GET|HEAD | admin/email/verify/{id}        | verification.verify | App\Http\Controllers\Auth\VerificationController@verify                | web,auth,signed,e:6,1 |
|        | GET|HEAD | admin/login                    | login               | App\Http\Controllers\Auth\LoginController@showLoginForm                | web,|
|        | POST     | admin/login                    |                     | App\Http\Controllers\Auth\LoginController@login                        | web,|
|        | POST     | admin/logout                   | logout              | App\Http\Controllers\Auth\LoginController@logout                       | |
|        | POST     | admin/password/email           | password.email      | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail  | web,|
|        | GET|HEAD | admin/password/reset           | password.request    | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web,|
|        | POST     | admin/password/reset           | password.update     | App\Http\Controllers\Auth\ResetPasswordController@reset                | web,|
|        | GET|HEAD | admin/password/reset/{token}   | password.reset      | App\Http\Controllers\Auth\ResetPasswordController@showResetForm        | web,|
|        | GET|HEAD | admin/register                 | register            | App\Http\Controllers\Auth\RegisterController@showRegistrationForm      | web,|
|        | POST     | admin/register                 |                     | App\Http\Controllers\Auth\RegisterController@register                  | web,|
|        | GET|HEAD | api/user                       |                     | Closure                                                                | api,|
|        | GET|HEAD | {brand}                        | main_landing        | App\Http\Controllers\BrandsLandingController@index                     | |
|        | GET|HEAD | {brand}/email/resend           | verification.resend | App\Http\Controllers\Auth\VerificationController@resend                | web,auth,throttle:
|        | GET|HEAD | {brand}/email/verify           | verification.notice | App\Http\Controllers\Auth\VerificationController@show                  | web,auth                 |        | GET|HEAD | {brand}/email/verify/{id}      | verification.verify | App\Http\Controllers\Auth\VerificationController@verify                | web,auth,signed,throttl|        | GET|HEAD | {brand}/login                  | login               | App\Http\Controllers\Auth\LoginController@showLoginForm                | web,guest                    |        | POST     | {brand}/login                  |                     | App\Http\Controllers\Auth\LoginController@login                        | web,guest                    |        | POST     | {brand}/logout                 | logout              | App\Http\Controllers\Auth\LoginController@logout                       | web                          |        | POST     | {brand}/password/email         | password.email      | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail  | web,guest                    |        | GET|HEAD | {brand}/password/reset         | password.request    | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web,guest                    |        | POST     | {brand}/password/reset         | password.update     | App\Http\Controllers\Auth\ResetPasswordController@reset                | web,guest                    |        | GET|HEAD | {brand}/password/reset/{token} | password.reset      | App\Http\Controllers\Auth\ResetPasswordController@showResetForm        | web,guest                    |        | GET|HEAD | {brand}/register               | register            | App\Http\Controllers\Auth\RegisterController@showRegistrationForm      | web,guest                    |        | POST     | {brand}/register               |                     | App\Http\Controllers\Auth\RegisterController@register                  | web,guest                    

```

  • Having no errors on the different routes.

  • Can differentiate the routes name depending on the prefix like front1.login, front2.login, admin.login ...

Thanks in advance



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire