简体   繁体   English

Silex app-> redirect与路由不匹配

[英]Silex app->redirect does not match routes

Let my application run on localhost, the path is: localhost/silex/web/index.php , defined routes as in the code below, I'd expect visiting localhost/silex/web/index.php/redirect redirects me to localhost/silex/web/index.php/foo and displays 'foo'. 让我的应用程序在localhost上运行,路径是: localhost/silex/web/index.php ,定义路由如下面的代码,我希望访问localhost/silex/web/index.php/redirect 重定向localhost/silex/web/index.php/foo并显示'foo'。 Instead it redirects me to localhost/foo . 相反,它将我重定向到localhost/foo

I am new to Silex and maybe I got it all wrong. 我是Silex的新手,也许我弄错了。 Could someone explain where is the problem? 有人可以解释问题出在哪里吗? Is it correct behavior and it should redirect for absolute paths? 它是正确的行为,它应该重定向绝对路径? Thanks. 谢谢。

<?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();

The redirect url expects an url to redirect to, not an in-app route. redirect网址需要重定向到网址,而不是应用内路由。 Try it this way: 试试这种方式:

$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"));
});

For internal redirects, which does not change the requested URL, you can also use a sub-request: 对于不会更改请求的URL的内部重定向,您还可以使用子请求:

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

$app->get('/redirect', function() use ($app) {
   $subRequest = Request::create('/foo');
   return $app->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
});

See also Making sub-Requests . 另请参阅制作子请求

Up to "silex/silex": ">= 2.0" , a native trait allow you to generate an URL based on the route name. 直到"silex/silex": ">= 2.0" ,本机特征允许您根据路由名称生成URL。

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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM