Up to "silex/silex": ">= 2.0"
, a native trait allow you to generate an URL based on the route name.
You can replace :
$app['url_generator']->generate('my-route-name');
By :
$app->path('my-route-name');
Then use it to redirect :
$app->redirect($app->path('my-route-name'));
Another possibility is to create a custom trait to directly redirect with a route name :
namespace Acme;
trait RedirectToRouteTrait
{
public function redirectToRoute($routeName, $parameters = [], $status = 302, $headers = [])
{
return $this->redirect($this->path($routeName, $parameters), $status, $headers);
}
}
Add the trait to your application definition :
use Silex\Application as BaseApplication;
class Application extends BaseApplication
{
use Acme\RedirectToRouteTrait;
}
Then use it wherever you need it :
$app->redirectToRoute('my-route-name');