Question

I recently started using Silex. I have been grouping certain functionality of my application in a separate ControllerProviderInterace (such as putting login and register together).

My issue is that these ControllerProviders are 'mounted' under a sub-url, such as:

$app->mount('/account', new Controller\Account() );

How would I 'alias', rewrite, or map certain urls to other urls. For instance, I would like the following mapping:

/login -> /account/login
Was it helpful?

Solution

You could do something like this:

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;

$app->match('/login', function (Request $request) use ($app) {
    $subRequest = $request->duplicate(null, null, null, null, null, array('REQUEST_URI' => '/account/login'));
    return $app->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
});

I haven't actually tested this, so you may have to adjust it. But that's the approach I would take. Basically a forwarding controller.

OTHER TIPS

I don't know if the ship has sailed on this, but you can mount right under the root. For example...

$app->mount('/', new AuthenticationControllerProvider());

Then in AuthenticationControllerProvider, you can specify the routes:

$app->get('/login', function () use ($app) {
    // do login things
});

$app->get('/register', function () use ($app) {
    // do register things
});

If you wanted other routes to point to those you could set up routes that redirect with 301 to these.

I hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top