skip to Main Content

I have 2 kind of routes. For Admin, all of my routes are always have an admin prefix. For User, it doesn’t have any prefix at all.

I’m able check if the currect route has admin prefix by using this code:

Route::current()->getPrefix() === '/admin'

But when I tried to do the same for non-prefix (the User) routes, it doesn’t work:

Route::current()->getPrefix() === '/'

How am I supposed to check if the current route doesn’t have prefix? I know I can technically just reverse the logic by using:

Route::current()->getPrefix() !== '/admin'

But I wanted something different so that I don’t get confused easily in the future. Thank you.

2

Answers


  1. As described in my comment, you could extend the Route Facade like follows. This enables you to check the admin prefix like that CustomRoute::hasPrefix("/admin");

        <?php
        
        namespace <YOUR NAMESPACE HERE>;
        
        use IlluminateSupportFacadesRoute;
        
        class CustomRoute extends IlluminateSupportFacadesRoute
        {
          public static function hasPrefix(String $prefix) : bool{
            return Route::current()->getPrefix() === $prefix;
          }
    
          public static function doesntHavePrefix(String $prefix) : bool{
            return !self::hasPrefix($prefix);
          }
        }
    
    Login or Signup to reply.
  2. Easiest way might be

    {{ !(Route::is('admin/index')) }}
    !(Request::routeIs('welcome'));
    

    There is already simmilar topic:
    using Request::is() function with named Routes Laravel

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search