Silex app-> redirect与路由不匹配

use*_*435 8 php redirect silex

让我的应用程序在localhost上运行,路径是:localhost/silex/web/index.php,在下面的代码中定义路由,我希望访问localhost/silex/web/index.php/redirect 重定向到我localhost/silex/web/index.php/foo并显示'foo'.相反,它将我重定向到localhost/foo.

我是Silex的新手,也许我弄错了.有人可以解释问题出在哪里吗?它是正确的行为,它应该重定向绝对路径?谢谢.

<?php

require_once __DIR__.'/../vendor/autoload.php';

use Symfony\Component\HttpFoundation\Response;

$app = new Silex\Application();

$app['debug'] = true;

$app->get('/foo', function() {
    return new Response('foo');
});

$app->get('/redirect', function() use ($app) {
    return $app->redirect('/foo');
});


$app->run();
Run Code Online (Sandbox Code Playgroud)

Mae*_*lyn 23

redirect网址期待一个URL重定向到,没有一个在应用程序的路线.试试这种方式:

$app->register(new Silex\Provider\UrlGeneratorServiceProvider());

$app->get('/foo', function() {
    return new Response('foo');
})->bind("foo"); // this is the route name

$app->get('/redirect', function() use ($app) {
    return $app->redirect($app["url_generator"]->generate("foo"));
});
Run Code Online (Sandbox Code Playgroud)