Skip to main content

Using Aura.Dispatcher in Silex

·527 words·3 mins
Hari KT
Author
Hari KT
Freelancer | Founder of Tripti and Tanvish | Maintainer of Aura for PHP, created by Paul M Jones

2 days back Paul M Jones wrote an awesome post A Peek At Aura v2 – Aura.Dispatcher the idea behind Aura.Dispatcher and how it was born.

So today, let us try to integrate Aura.Dispatcher with Silex. This post is inspired by the comment made by Luis Cordova. Thank you.

1
2
3
composer create-project silex/silex --no-dev silexproject
cd silexproject
composer require aura/dispatcher dev-develop-2

I hope you have composer installed else get composer.

I am not going to explain each and everything, the code is self explanatory. You can move the classes according to your wish (may be to another folder). I am trying to show a simple use case.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
require __DIR__ . '/vendor/autoload.php';

use Aura\Dispatcher\Dispatcher;

$dispatcher = new Dispatcher;

$dispatcher->setMethodParam('action');
$dispatcher->setObjectParam('controller');

class Blog
{
    public function browse()
    {
        // ...
    }

    public function hello($name, $app)
    {
        return 'Hello '. ucfirst($app->escape($name));
    }

    public function edit($id)
    {
        echo "Here";
        exit;
        // ...
    }

    public function add()
    {
        // ...
    }

    public function delete($id)
    {
        // ...
    }
}

$dispatcher->setObject('blog', function () {
    return new Blog;
});

$app = new Silex\Application();

$app->get('/hello/{name}', function ($name) use ($dispatcher, $app) {
    $params = [
        'controller' => 'blog',
        'action' => 'hello',
        'name' => $name,
        'app' => $app,
    ];
    $result = $dispatcher->__invoke($params);    
    return $result;
});

$app->run();

See how we moved the return 'Hello '. ucfirst($app->escape($name)); to a controller and action. I haven’t used Silex extensively, so there can be better ways for integration.

Update : I was asking Beau D. Simensen on the integration, and he gave another shot.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
// all code as same as above, upto the route

$app->get('/hello/{name}', function ($name) use ($app) {
    return [
        'controller' => 'blog',
        'action' => 'hello',
        'name' => $name,
        'app' => $app,
    ];
});

$app->on(\Symfony\Component\HttpKernel\KernelEvents::VIEW, function ($event) use ($app, $dispatcher) {
    $view = $event->getControllerResult();

    if (is_null($view) || is_string($view)) {
        return;
    }
    
    if ( ! is_array($view)) {
        // we can only handle array data in the view
        return;
    }
    
    if (! (isset($view['controller']) && isset($view['action']))) {
        // at this point we don't know what is going on.
        return;
    }

    $response = $dispatcher($view);

    if ( ! $response instanceof \Symfony\Component\HttpFoundation\Response) {
        // If the response is not a Response instance, wrap it in one
        // and assume that it was something appropriate as a response
        // body.
        $response = new \Symfony\Component\HttpFoundation\Response($response);
    }

    $event->setResponse($response);
});

$app->run();

I have purposefully kept the full path like \Symfony\Component\HttpFoundation\Response

Hope you love Aura.Dispatcher and use when you need architecural changes.

Please do take time to read Refactoring To Architecture Changes

Thank you and if you loved the post please do a tweet :-).

Happy PhPing!

Related

Looking at Aura v2

·919 words·5 mins
If you have noticed recently, there have been tons of commits from Paul M Jones for aura version 2. More standalone repos born. Everyone should try and give feedback as much as possible. Interesting stuffs split from Aura.Sql v 1.3 are Extended PDO, As the name says, it is an extended version of PDO. Good thing is it is PHP 5.3 compatible. If you have worked with PDO you know the good and bad. One of the difficulty is, it cannot use an array for an in clause.

Aura Framework Released

·67 words·1 min
It is a great feeling, and one of the happiest moment in my life when aura releases its stable framework. Aura is one of my first framework, which helped me to learn the inner working of a framework, how to create a framework, rather than playing with full stack framework. If you are interested to learn or contribute in the next version of Aura join the group.

The Book on Aura Moves to the Manual

·126 words·1 min
Some days back I wrote about a WIP of The Book on Aura It got some attention and pull requests. Akihito Koriyama have already started to convert the documentation to Japanese. And today I am happy to announce that it is officially going to the manuals. Contributing # Fork https://github.com/auraphp/auraphp.github.com and contribute to the manuals. The manuals are in different languages. Currently Akihito Koriyama is working on Japanese translation.