If Laravel is installed in a subfolder of a domain and the routes are cached the route for "/" is not working. Without routes caching the route is working as expected.
The expected behaviour is, that route caching does not change the handling of routes. With and without caching should result in the same behaviour.
Maybe the root cause is the same as #32228 or #31768.
ln -s . testhttp://localhost/testOpen "http://localhost/test" in a browser. It works as expected
php artisan route:cacheOpen "http://localhost/test" in a browser. It throws an exception:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods: HEAD.
Other routes, e.g. /login are working.
php artisan route:clearIt works again.
Thanks for getting in touch, but installing Laravel in a sub-folder is not supported.
I don't know what your route file look like but I think's it is not laravel's problem. Usually web server don't follow sym links due to security reason. Symbolic links can be configured in your web server such as nginx like:
disable_symlinks off;
Interessting that it is not supported. The setup was working till Laravel 7.0.8. Then the commits 3d58cd9 and ac6f3a8 introduced the problem.
@mo3000 as the setup is working without caching I think it is not related to the webserver.
For reference, I got this working with a custom middleware:
public function handle(Request $request, Closure $next)
{
if ($request->path() === '/') {
$newURI = $request->server->get('REQUEST_URI') . 'index.php';
$request->server->set('REQUEST_URI', $newURI);
}
return $next($request);
}
This appends index.php to the requested uri and prevents the removal of the '/' suffix.
@GrahamCampbell I don't see where in the documentation it is shown that placing laravel in a subfolder is unsupported. I can only find that there is a security concern in directory configuration (which can be avoided).
The route caching did work for laravel version 5.x -> 7.0.8 and is now only broken for the '/' route and not for any other routes. The breaking change is introduced in ac6f3a8 as mentioned by @berndulum .
The issue seems to be located in src/Illuminate/Routing/CompiledRouteCollection.php
protected function requestWithoutTrailingSlash(Request $request)
...
$trimmedRequest->server->set(
'REQUEST_URI', rtrim($parts[0], '/').(isset($parts[1]) ? '?'.$parts[1] : '')
);
...
This modifies the request object and messes up both the pathInfo and basePath (when using laravel in a subfolder)
Before protected function requestWithoutTrailingSlash(Request $request)
Illuminate\Http\Request {
...
pathInfo: "/"
requestUri: "/lar73/"
baseUrl: "/lar73"
basePath: "/lar73"
method: "GET"
format: "html"
...
}
After protected function requestWithoutTrailingSlash(Request $request)
Illuminate\Http\Request {
...
pathInfo: "/lar73"
requestUri: "/lar73"
baseUrl: ""
basePath: ""
method: "GET"
format: "html"
...
}
It's unclear to me if this is intended behaviour. I dont know how to continue from here as I'm unsure if laravel in "subfolders" is unsupported or not.
Just wanted to throw some extra weight behind this as well. I have the exact same problem as my organization runs different applications on different "subfolders" for load balancer routing purposes.
Since the upgrade to Laravel 7.x everything works fine until we do a php artisan route:cache and suddenly our root '/' GET route is broken and shows the following in the browser:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods: HEAD.
The route:list still looks correct after caching. Our APP_URL is APP_URL=https://mydomain.local/subfolder
+------------------+----------------------------------------+-----------------------------------------+-----------------------------+-------------------------------------------------------------------------+------------------------------------------------------+
| Domain | Method | URI | Name | Action | Middleware |
+------------------+----------------------------------------+-----------------------------------------+-----------------------------+-------------------------------------------------------------------------+------------------------------------------------------+
| mydomain.local | GET|HEAD | /
Same problem as in #32832
I found a solution to bypass the problem. If you add the route "/your_subfolder" instead of "/" in your web.php it should work with the caching.
I think Laravel members want to close their eyes on this problem. I CAN'T believe that Laravel 7.x in a sub-folder is not supported. (EDIT I should have said: subfolder website is not supported)
I also think GrahamCampbell closed this issue a little bit too fast.
This issue should be Re-open
Hey @jrbecart. It's true, we don't support Laravel in a sub directory. See https://laravel.com/docs/8.x#directory-configuration
Hi @driesvints, I don't see where it says it's not supported.
Like others you pointed out to this part of the doc, but it doesn't say we can't have multiple websites on the same server in different directories.
Here is what you point out from Laravel doc:
Directory Configuration
Laravel should always be served out of the root of the "web directory" configured for your web server. You should not attempt to serve a Laravel application out of a subdirectory of the "web directory". Attempting to do so could expose sensitive files present within your application.
But what we mean here it's to have: (also called subfolders websites)
https://domain1.com/website1
https://domain1.com/website2
https://domain1.com/website3
On the server side the root web directory of each website is, for example:
root /var/www/website1/public;
root /var/www/website2/public;
root /var/www/website3/public;
So I don't see here where there is something wrong with that, and the doc doesn't say it's not supported!
Please explain us why we can't have website1, website2, website3 on the same server, in different directories, served out of the root of the "web directory".
Also it works fine without caching.
Also it was working fine before with caching.
Regards,
@jrbecart think you're just confusing the term "subfolder" to be different from "subdirectory" which it is not. It's pretty explicitly explained in the section I linked to.
@driesvints no I am not confusing, but the doc is not explicit as you said. Without mentioning subfolder or subdirectory, what we are referring to here is: can we have website like https://domain1.com/website/ working with laravel and the caching option enabled, because without caching it works.
If a laravel website is not possible in https://domain1.com/website/ it should be more explicit in the doc:, like:
You can't have a website under https://yourdomain/website
And if it's really the case it's unbelievable, because many many websites and web apps, are like that.
Can we have a confirmation/explanation from another laravel team member?
And please, please let us know why:
it works fine without caching?
it was working fine before with caching?
Throwing some additional support behind @jrbecart here. I agree it is absolutely not explicit in the linked documentation.
To further complicate the matter it has worked fine until Laravel 7.x and still works fine in Laravel 7.x as long as you do not use cached routes. I can't see any way that the framework team could disagree that there is a bug present.
At the very least the discrepancy between cached/non-cached routes is a bug that needs dealt with.
Most helpful comment
For reference, I got this working with a custom middleware:
This appends
index.phpto the requested uri and prevents the removal of the '/' suffix.