# How to get current route?

**URL:** <https://discourse.slimframework.com/t/how-to-get-current-route/219>\
**Category:** Questions\
**Created:** [May 8, 2016, 4:42am UTC](https://discourse.slimframework.com/t/how-to-get-current-route/219 "2016-05-08T04:42:01Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![daxing](https://avatars.discourse-cdn.com/v4/letter/d/9f8e36/32.png) [@daxing](https://discourse.slimframework.com/u/daxing)\
**Post date:** [May 8, 2016, 4:42am UTC](https://discourse.slimframework.com/t/how-to-get-current-route/219/1 "2016-05-08T04:42:01Z")

</div>

I have tried the following method. But it returns null. What do I do?

use Slim\Http\Request;  
use Slim\Http\Response;  
use Slim\App;

$app = new App([  
‘settings’ =\> [  
// Only set this if you need access to route within middleware  
’determineRouteBeforeAppMiddleware’ =\> true  
]  
])

// routes…  
$app-\>add(function (Request $request, Response $response, callable $next) {  
$route = $request-\>getAttribute(‘route’);  
$name = $route-\>getName();  
$groups = $route-\>getGroups();  
$methods = $route-\>getMethods();  
$arguments = $route-\>getArguments();

```
// do something with that information

return $next($request, $response);

```

});

---

<div class="post-metadata">

**Author:** ![JoeBengalen](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@JoeBengalen](https://discourse.slimframework.com/u/JoeBengalen)\
**Post date:** [May 8, 2016, 9:48am UTC](https://discourse.slimframework.com/t/how-to-get-current-route/219/2 "2016-05-08T09:48:16Z")

</div>

I have just tried the following code and it works like a charm:

```php
<?php
// index.php

require 'vendor/autoload.php';

use Slim\Http\Request;
use Slim\Http\Response;
use Slim\App;

$app = new App([
    'settings' => [
        // Only set this if you need access to route within middleware
        'determineRouteBeforeAppMiddleware' => true,
    ]
]);

// routes...
$app->add(function (Request $request, Response $response, callable $next) {
    $route = $request->getAttribute('route');

    var_dump($route->getMethods());

    return $next($request, $response);

});

$app->get('/', function (Request $request, Response $response) {
    $response->write('from home');
});
$response = $app->run();

```

Output:

```auto
array (
  0 => 'GET',
)
from home

```

---

<div class="post-metadata">

**Author:** ![daxing](https://avatars.discourse-cdn.com/v4/letter/d/9f8e36/32.png) [@daxing](https://discourse.slimframework.com/u/daxing)\
**Post date:** [May 9, 2016, 6:35pm UTC](https://discourse.slimframework.com/t/how-to-get-current-route/219/3 "2016-05-09T18:35:47Z")

</div>

not working.

$name = $route-\>getName();  
is still null.

---

<div class="post-metadata">

**Author:** ![TheLobos](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@TheLobos](https://discourse.slimframework.com/u/TheLobos)\
**Post date:** [May 9, 2016, 8:54pm UTC](https://discourse.slimframework.com/t/how-to-get-current-route/219/4 "2016-05-09T20:54:35Z")

</div>

I use this code in my app:

```
$route = $request->getAttribute('route');
$rName = $route ? $route->getName() : null;

```

If this returns null, then this means that your request does not match an existing route (basically this means you’re having a 404 condition).

---

<div class="post-metadata">

**Author:** ![JoeBengalen](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@JoeBengalen](https://discourse.slimframework.com/u/JoeBengalen)\
**Post date:** [May 10, 2016, 7:23am UTC](https://discourse.slimframework.com/t/how-to-get-current-route/219/5 "2016-05-10T07:23:49Z")

</div>

Did you exactly copy my code?

Can you show your route?

---

<div class="post-metadata">

**Author:** ![daxing](https://avatars.discourse-cdn.com/v4/letter/d/9f8e36/32.png) [@daxing](https://discourse.slimframework.com/u/daxing)\
**Post date:** [May 10, 2016, 7:39am UTC](https://discourse.slimframework.com/t/how-to-get-current-route/219/6 "2016-05-10T07:39:46Z")

</div>

$settings = [  
‘settings’ =\> [  
‘determineRouteBeforeAppMiddleware’ =\> true,  
‘displayErrorDetails’ =\> SLIM\_DISPLAY\_ERROR\_DETAILS  
]  
];  
$app = new Slim\App($settings);

$app-\>add(function ($request, $response, $next) {  
$route = $request-\>getAttribute(‘route’);

```
$rName = $route ? $route->getName() : null;

error_log($rName);//not working, still null, other methods like getMethods works...

$uri=$request->getUri();//this works
$path=$uri->getPath();//this works

$response = $next($request, $response);
return $response;

```

});

$app-\>get(’/hello/{name}’, function ($request, $response, $args) {  
$response-\>withJson(array(“Hello”=\>$args[‘name’]));  
return $response;  
});

---

<div class="post-metadata">

**Author:** ![daxing](https://avatars.discourse-cdn.com/v4/letter/d/9f8e36/32.png) [@daxing](https://discourse.slimframework.com/u/daxing)\
**Post date:** [May 10, 2016, 7:41am UTC](https://discourse.slimframework.com/t/how-to-get-current-route/219/7 "2016-05-10T07:41:55Z")

</div>

And thank you Joe for helping 🙂

---

<div class="post-metadata">

**Author:** ![JoeBengalen](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@JoeBengalen](https://discourse.slimframework.com/u/JoeBengalen)\
**Post date:** [May 10, 2016, 8:08am UTC](https://discourse.slimframework.com/t/how-to-get-current-route/219/8 "2016-05-10T08:08:15Z")

</div>

You didnt give your route a name … So you cannot retrieve it.

Call `setName` on the route:

```php
$app->get('/hello/{name}', function ($request, $response, $args) {
    $response->withJson(array("Hello"=>$args['name']));
    return $response;
})->setName('hello');

```

---

<div class="post-metadata">

**Author:** ![daxing](https://avatars.discourse-cdn.com/v4/letter/d/9f8e36/32.png) [@daxing](https://discourse.slimframework.com/u/daxing)\
**Post date:** [May 10, 2016, 8:13am UTC](https://discourse.slimframework.com/t/how-to-get-current-route/219/9 "2016-05-10T08:13:55Z")

</div>

ohhhh!!! that works.  
But is there any way to get ‘/hello/{name}’ directly without setting the name?

---

<div class="post-metadata">

**Author:** ![JoeBengalen](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@JoeBengalen](https://discourse.slimframework.com/u/JoeBengalen)\
**Post date:** [May 10, 2016, 8:15am UTC](https://discourse.slimframework.com/t/how-to-get-current-route/219/10 "2016-05-10T08:15:08Z")

</div>

does `getPattern` what you are looking for?

---

<div class="post-metadata">

**Author:** ![daxing](https://avatars.discourse-cdn.com/v4/letter/d/9f8e36/32.png) [@daxing](https://discourse.slimframework.com/u/daxing)\
**Post date:** [May 10, 2016, 8:20am UTC](https://discourse.slimframework.com/t/how-to-get-current-route/219/11 "2016-05-10T08:20:32Z")

</div>

> [@JoeBengalen](#):
>
> getPattern

You are the best!!! It works!!!
