Laravelista\Ekko\Ekko::isActiveRoute PHP Method

isActiveRoute() public method

Any section of the route name can be replaced with a * wildcard. Example: user.*
public isActiveRoute ( string $routeName, string $output = "active" ) : boolean
$routeName string
$output string
return boolean
    public function isActiveRoute($routeName, $output = "active")
    {
        if (strpos($routeName, '*') !== false) {
            // Quote all RE characters, then undo the quoted '*' characters to match any
            // sequence of non-'.' characters.
            $regex = '/^' . str_replace(preg_quote('*'), '[^.]*?', preg_quote($routeName, '/')) . '$/';
            if (preg_match($regex, $this->route->currentRouteName())) {
                return $output;
            }
        } elseif ($this->route->currentRouteName() == $routeName) {
            return $output;
        }
        return null;
    }

Usage Example

Example #1
0
 /** @test */
 public function it_detects_active_route_by_name()
 {
     $router = m::mock(\Illuminate\Routing\Router::class);
     $router->shouldReceive('currentRouteName')->times(8)->andReturn('users.index');
     $url = m::mock(\Illuminate\Routing\UrlGenerator::class);
     $ekko = new Ekko($router, $url);
     $this->assertEquals("active", $ekko->isActiveRoute('users.index'));
     $this->assertEquals("hello", $ekko->isActiveRoute('users.index', 'hello'));
     $this->assertEquals(null, $ekko->isActiveRoute('clients.index'));
     $this->assertEquals(null, $ekko->isActiveRoute('clients.index', 'hello'));
     // Wildcard support
     $this->assertEquals("active", $ekko->isActiveRoute('users.*'));
     $this->assertEquals("hello", $ekko->isActiveRoute('users.*', 'hello'));
     $this->assertEquals(null, $ekko->isActiveRoute('clients.*'));
     $this->assertEquals(null, $ekko->isActiveRoute('clients.*', 'hello'));
 }