Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added dispatcher::beforeForward event #12857

Merged
merged 3 commits into from
May 21, 2017
Merged

Added dispatcher::beforeForward event #12857

merged 3 commits into from
May 21, 2017

Conversation

sergeyklay
Copy link
Contributor

@sergeyklay sergeyklay commented May 21, 2017

Hello!

In raising this pull request, I confirm the following:

  • I have read and understood the Contributing Guidelines?
  • I have checked that another pull request for this purpose does not exist.
  • I wrote some tests for this PR.

Small description of change: Added dispatcher::beforeForward event to allow forwarding request to the separated module.

use Phalcon\Events\Event;
use Phalcon\Mvc\Dispatcher;
use App\Backend\Bootstrap as Backend;
use App\Frontend\Bootstrap as Frontend;

// Registering modules
$modules = [
    'frontend' => [
        'className' => Frontend::class,
        'path'      => __DIR__ . '/app/Modules/Frontend/Bootstrap.php',
        'metadata'  => [
            'controllersNamespace' => 'App\Frontend\Controllers',
        ],
    ],
    'backend' => [
        'className' => Backend::class,
        'path'      => __DIR__ . '/app/Modules/Backend/Bootstrap.php',
        'metadata'  => [
            'controllersNamespace' => 'App\Backend\Controllers',
        ],
    ],
];

$application->registerModules($modules);

// Setting beforeForward listener
$eventsManager = $di->getShared('eventsManager');

$eventsManager->attach(
    'dispatch:beforeForward',
    function(Event $event, Dispatcher $dispatcher, array $forward) use ($modules) {
        $metadata = $modules[$forward['module']]['metadata'];

        $dispatcher->setModuleName($forward['module']);
        $dispatcher->setNamespaceName($metadata['controllersNamespace']);
    }
);

// Forward
$this->dispatcher->forward(
    [
        'module'     => 'backend',
        'controller' => 'posts',
        'action'     => 'index',
    ]
);

Thanks

@sergeyklay sergeyklay merged commit 4d68d8a into phalcon:3.2.x May 21, 2017
@sergeyklay sergeyklay deleted the feature/mvc/dispatcher/before_forward branch May 21, 2017 10:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant