简体   繁体   English

Slim (V3) 框架:为生成的链接添加前缀,但不为传入路由添加前缀

[英]Slim (V3) Framework: Add a prefix to generated links, but not to incoming routes

I have basically this exact question Add a prefix to generated links, but not to incoming routes but for Slim V3.我基本上有这个确切的问题为生成的链接添加前缀,但不是添加到传入路由,而是用于 Slim V3。 The short version of the question is:问题的简短版本是:

How can I add a prefix to generated links (eg those generated with $router->pathFor('home'), say) but NOT include this prefix as part of the routing.如何为生成的链接添加前缀(例如,使用 $router->pathFor('home') 生成的链接)但不将此前缀作为路由的一部分包含在内。

In my situation I have a front-end proxy which is responsible for routing to my application in a Docker container (although how that is set up is irrelevant).在我的情况下,我有一个前端代理,它负责路由到 Docker 容器中的应用程序(尽管它的设置方式无关紧要)。

I need the links that are ultimately sent back to the browser to include the prefix, but for them to be ignored in the routes as the proxy server removes them when it does the passthrough.我需要最终发送回浏览器的链接以包含前缀,但它们在路由中被忽略,因为代理服务器在执行传递时删除它们。

Class Slim\\Router seems to have a basePath which could be set by calling setBasePath , but it seems this basePath is not useful in your case. Class Slim\\Router似乎有一个basePath可以通过调用setBasePath来设置,但似乎这个 basePath 在你的情况下没有用。 You can have your own Router class with a custom pathFor method capable of prefixing your paths generated for a named route, then you can replace Slim's default router with yours.您可以使用自定义pathFor方法拥有自己的Router类,该方法能够为为命名路由生成的路径添加前缀,然后您可以将 Slim 的默认router替换为您的。 Here is a fully functional example:这是一个功能齐全的示例:

// declare your class and change pathFor behavior
class MyPrefixAwareRouter extends Slim\Router {
    private $prefix = '';
    public function setPrefix($prefix = '') {
        $this->prefix = $prefix;
    }
    public function pathFor($name, array $data = [], array $queryParams = [])
    {
        return $this->prefix . parent::pathFor($name, $data, $queryParams);
    }
}

$container = new Slim\Container;

// custom path prefix for all named routes
$container['route-prefix'] = '/some/prefix/to/be/removed/by/proxy';

// router setup, copied from Slim\DefaultServicesProvider.php
// with slight change to call setPrefix
$container['router'] = function ($container) {
    $routerCacheFile = false;
    if (isset($container->get('settings')['routerCacheFile'])) {
        $routerCacheFile = $container->get('settings')['routerCacheFile'];
    }
    $router = (new MyPrefixAwareRouter)->setCacheFile($routerCacheFile);
    if (method_exists($router, 'setContainer')) {
        $router->setContainer($container);
    }
    $router->setPrefix($container->get('route-prefix'));

    return $router;
};
$app = new \Slim\App($container);

$app->get('/sample', function($request, $response, $args){
    return "From request: " . $request->getUri()->getPath() . "\nGenerated by pathFor: " . $this->router->pathFor('sample-route');
})->setName('sample-route');
// Run app
$app->run();

If you visit <your-domain>/sample the output will be:如果您访问<your-domain>/sample ,输出将是:

From request: /sample
Generated by pathFor: /some/prefix/to/be/removed/by/proxy/sample

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

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