# SLIM framework - Semi-Dynamic routing

**URL:** <https://discourse.slimframework.com/t/slim-framework-semi-dynamic-routing/1508>\
**Category:** Questions\
**Created:** [May 23, 2017, 7:05am UTC](https://discourse.slimframework.com/t/slim-framework-semi-dynamic-routing/1508 "2017-05-23T07:05:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcelloh](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/marcelloh/32/404_2.png) [@marcelloh](https://discourse.slimframework.com/u/marcelloh)\
**Post date:** [May 23, 2017, 7:05am UTC](https://discourse.slimframework.com/t/slim-framework-semi-dynamic-routing/1508/1 "2017-05-23T07:05:55Z")

</div>

I was playing with the Slim framework and saw that for each action inside each controller, I had to duplicate stuff inside my router configuration.  
I wanted a more “dynamic” approach, so I came up with the following.  
I want to know if I overlooked something

**Inside routers**  
_(This is where only the controllers are registered)_

```
$app->any('/admin[/{args:.*}]', function ($request, $response, $args) {
    $controller = new AdminController($this, $request, $response);
    return $controller->handle($args);
});
$app->any('/help[/{args:.*}]', function ($request, $response, $args) {
    $controller = new HelpController($this, $request, $response);
    return $controller->handle($args);
});
$app->any('[/{args:.*}]', function ($request, $response, $args) {
    $controller = new FrontendIndexController($this, $request, $response);
    return $controller->handle($args);
});

```

**inside AdminController**  
_(This is an example controller, with only the actions)_

```
class AdminController extends BaseController
{
    /**
     * Return the page: index
     */
    public function index()
    {
        return $this->view->render($this->response, $this->templateDir . '/index.twig', []);
    }

    /**
     * Return the page: page2
     */
    public function page2($args)
    {
        return $this->view->render($this->response, $this->templateDir . '/page2.twig', []);
    }
}

```

**inside BaseController**  
_(This is where the “dynamic” part is)_

```
/**
 * Handles the routing inside the controller
 *
 * @param $args - all url arguments
 */
public function handle($args)
{
    $args = $this->getArgs($args);

    if (empty($args[0]))
    {
        $this->index();
    }
    else
    {
        try
        {
            $this->{$args[0]}(...$args);
        }
        catch (\Error $e)
        {
            $this->error($e->getMessage());
        }
    }
}

private function getArgs($args)
{
    if (!empty($args))
    {
        $args = explode("/", $args['args']);
    }
    return $args;
}

private function error($message, $args)
{
    echo 'Error:<BR>' . $message. '<HR>';
    var_dump($args);
}
```

---

<div class="post-metadata">

**Author:** ![JupiterN](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@JupiterN](https://discourse.slimframework.com/u/JupiterN)\
**Post date:** [May 25, 2017, 12:03pm UTC](https://discourse.slimframework.com/t/slim-framework-semi-dynamic-routing/1508/2 "2017-05-25T12:03:23Z")

</div>

Hi,

I have done something similar in [https://github.com/jupitern/slim3-skeleton](https://github.com/jupitern/slim3-skeleton) and added container and automatic resolution. take a look.

In my opinion the “handle” method should not be in the contoller or it should be a static method in BaseController for example.
