1

I'm using this method for using different controller for my route but didn't work

here is my route code

Route::get('{slug}', ['middleware'=>'isPage', 'uses'=>'PageController@view'])->name('viewPage');


Route::get('{modelName}', ['middleware'=>'isUser', 'uses'=>'ModelController@view'])->name('viewModel');

and here is middleware code for isUser

use Closure;

use App\ModelProfile;
class isUser{
 public function handle($request, Closure $next)
 {
    $slug = $request->route()->parameter('slug');
    $model = ModelProfile::where([
        ['slug', $slug],
        ['is_status', 'ACTIVE'],
        ['is_deleted', 'NO']
    ])->count();

    if($model > 0){
        return $next($request);
    } else {
        abort(404);
    }
}

and here is my middleware code for isPage

use Closure;

use App\Page;
class isPage{
 public function handle($request, Closure $next)
 {
    $slug = $request->route()->parameter('slug');
    $model = Page::where([
        ['slug', $slug],
        ['is_status', 'ACTIVE'],
        ['is_deleted', 'NO']
    ])->count();

    if($model > 0){
        return $next($request);
    }
 }
}

In kernel.php

'isPage' => \App\Http\Middleware\isPage::class,
'isUser' => \App\Http\Middleware\isUser::class,
Sunny Negi
  • 13
  • 4
  • Please read this if you find the solution - `https://github.com/laravel/framework/issues/27179` – Rashed Hasan Aug 30 '19 at 14:13
  • 1
    what happens if a page slug is exactly the same as a user slug ? What you're trying to do doesn't have a good solution. – N69S Aug 30 '19 at 14:26
  • @SunnyNegi, are these middlewares registered in `Kernel.php` with aliases? Also, you start your middleware class name with small letters. Is that the same with the class file name? – Udo E. Aug 30 '19 at 15:50
  • @UdoE yes these middlewares are registered in Kernel.php with same as class name / filename – Sunny Negi Sep 02 '19 at 01:25
  • @N69S no their is no possability coz when we generate slug, we put an unique check in database – Sunny Negi Sep 02 '19 at 01:26
  • `didn't work` - what does that mean? What did you try, exactly, and what happened? If you visit some URL like `/foo`, and that is not a slug, you will get a 404, right? You cannot have 2 wildcard routes, as the first one will always match everything. You need instead routes like `/pages/{slug}` and `/users/{modelName}`. – Don't Panic Sep 03 '19 at 09:07
  • @Don'tPanic , so how facebook will manage the url can you explain for profile and pages – Sunny Negi Sep 03 '19 at 09:33
  • Erm, because they have a completely different architecture? :-) – Don't Panic Sep 03 '19 at 09:35
  • Once a route matches, that is the route used. `next()` in a middleware passes the request to the next middleware, or to the next level of the application (eg a controller, or validation, etc), not to the next route. – Don't Panic Sep 03 '19 at 09:36

0 Answers0