问题
I need to configure a symfony app inside a subfolder of a hosted domain.
The app registers some route, for example /hello
.
The directory structure is:
/ (http root)
/myapp (symfony app)
/myapp/.htaccess (invisible redirect)
/myapp/public
/myapp/src
/myapp/vendor
/myapp/...
With Apache mod-rewrite I redirect internally all urls from www.mysite.test/myapp/...
to www.mysite.test/myapp/public/...
.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ myapp/public/$1 [L]
</IfModule>
Then when I browse the url www.mysite.test/myapp/hello
the app return no route for myapp/hello
.
There is a way to mount all routes with a custom prefix to solve this problem? In this way the controllers can handle all the url with the /myapp
prefix and also the url generated by route has this prefix.
Thanks.
回答1:
I got this error too few months ago and solved it.
Here is my thread about this same problem
Hope It can help you. If you have questions about my answers, fell free to ask.
回答2:
maybe I found a solution to the route not found error.
- I define a parameter
app.web_root_prefix
inservice.yaml
with the url prefix. - I change the definition of Kernel::loadRoutes on
Kernel.php
:
class Kernel extends BaseKernel
{
use MicroKernelTrait {
loadRoutes as _loadRoutes;
}
// [omitted code]
public function loadRoutes(LoaderInterface $loader)
{
$r = $this->_loadRoutes($loader);
$prefix = $this->getContainer()->getParameter('app.web_root_prefix');
if (!empty($prefix)) {
/** @var Route $route */
foreach ($r->getIterator() as $route) {
$path = $route->getPath();
if (strpos($path, $prefix) !== 0 && empty(array_diff($route->getSchemes(), array('http', 'https', 'ftp')))) {
// Add the prefix only if it is not already there.
$route->setPath('/'.$prefix.$path);
}
}
// $r->addPrefix($prefix);
}
return $r;
}
}
In this mode all routes loaded from a .yaml file are prefixed correctly.
Also you must change all paths in security.yaml inserting the prefix %app.web_root_prefix%
.
I must make more test...
来源:https://stackoverflow.com/questions/54402043/how-configure-php-symfony4-app-inside-site-subfolder-routing-problem