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,
9
  • Please read this if you find the solution - https://github.com/laravel/framework/issues/27179 Commented Aug 30, 2019 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. Commented Aug 30, 2019 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? Commented Aug 30, 2019 at 15:50
  • @UdoE yes these middlewares are registered in Kernel.php with same as class name / filename Commented Sep 2, 2019 at 1:25
  • @N69S no their is no possability coz when we generate slug, we put an unique check in database Commented Sep 2, 2019 at 1:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.