The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Slim 3 routing to a controller best practice

Jack Heuston's Avatar

Jack Heuston

29 Oct, 2015 03:05 PM

Hello!
I'm writing an app with Slim Framework 3, and I was wondering how I can achieve the following result.

I'm having many routes, and some of them can be satisfied by the same controller.

$this->slimApp->get('/', 'Controller\HomeController:home')->setName('home');
$this->slimApp->get('/test', 'Controller\HomeController:test')->setName('test');
And the HomeController has for example the method home():
public function home()
{
    // Do some stuff here... then render
    $this->render('home.twig');
}

The render() method comes from the BaseController (which HomeController extends).

While setting the routes, I'm also setting the following factories to have the HomeController have my main app object:

$this->slimApp->getContainer()['Controller\HomeController'] = function () {
    return new Controller\HomeController($this);
};
This lets me have in the controllers the main object of my app.

I was wondering, how can I pass the $request, $response and $arguments of a route as well? I'd like to have methods in my controllers that only takes care at most of the arguments they need (GET and POST params?) and $request and $response should go in the properties of the BaseController, so that I can use them if I really need them (render page in $response, or push CSRF tokens in it, coming from slim\csrf middleware)

Basically I'd like to avoid having to create controllers with tons of methods like this:

public function method1($req, $res, $args) {}
public function method2($req, $res, $args) {}
public function method3($req, $res, $args) {}
...

I'd prefer something more like this:

public function method1() {}
public function checkEmail($args) {}

The BaseController should take care of the request and response objects, pushing eventually CSRF tokens in the response, on top of what the more specific Controller would like to render.

I've been thinking at routing strategies and Middleware but I haven't found a clean solution yet.

Thank you!

  1. 1 Posted by Jack Heuston on 06 Nov, 2015 04:36 PM

    Jack Heuston's Avatar

    I solved this creating my own CallableResolver, implementing Slim\Interfaces\CallableResolverInterface and pushing it in the container of the app, key "callableResolver".

  2. 2 Posted by jd on 14 Nov, 2015 08:52 AM

    jd's Avatar

    Hi Jack,
    Would you mind sharing the skeleton of you solution?

  3. 3 Posted by Fred on 11 Dec, 2015 02:28 PM

    Fred's Avatar

    Hello Jack,
    Please, share your code with us.
    Thank you.

  4. 4 Posted by Jack Heuston on 11 Dec, 2015 02:31 PM

    Jack Heuston's Avatar

    Hello!
    Sorry I didn't see the replies earlier. During the weekend I'll share with you the interested code, since the app now is not a skeleton anymore.

  5. 5 Posted by nattyPablo on 03 Mar, 2016 04:14 AM

    nattyPablo's Avatar

    Hi Jack Heuston, did you ever get a chance to post your CallableResolver? If so, could you share the link?

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac