I am using laravel in a project. On my local machine the server I have to access is just
laraveltest.dev
. When I open this URL the project works fine and without problems.
However, when I upload this on a testing server, where the stuff is located in a sub-foder, like this: laraveltest.de/test2/
. The public folder is at laraveltest.de/test2/public/
, but when calling laraveltest.de/test2/public
the application always returns an 404 error.
I thought this might be because of the base path, so I did the following in the bootstrap/app.php
$app = new Laravel\Lumen\Application(
realpath(__DIR__.'/../') . env('APP_BASE_PATH')
);
where env('APP_BASE_PATH')
is the subfolder.
So app->basePath()
returns /var/www/laraveltest/test2/public
. However, when now opening
laraveltest.de/test2/public
I'm always getting the 404 error and I don't know why. What am I doing wrong?
You don't need to change basePath
, except if you use custom folder application structure. Kinda like this:
bootstrap
├── app.php
└── autoload.php
config
├── app.php
├── auth.php
├── cache.php
├── compile.php
[...]
src
└── Traviola
├── Application.php
├── Commands
│ └── Command.php
├── Console
│ ├── Commands
[...]
So, in your case, all you have to do is:
Check .htaccess configuration. Does server allow .htaccess
file to override specific path configuration?
Check your public/index.php
file. Change this line:
/*
|---------------------
| Run The Application
|---------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/
$app->run();
// into something like this
$app->run($app['request']);
Hope this helps.
If you wonder how Lumen does not work in subfolder. You may see Laravel\Lumen\Application::getPathInfo()
line 1359
. To make Lumen works in subfolder, change this method, just make a class that extends Laravel\Lumen\Application
.
<?php namespace App;
use Laravel\Lumen\Application as BaseApplication;
class Application extends BaseApplication
{
/**
* Get the current HTTP path info.
*
* @return string
*/
public function getPathInfo()
{
$query = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '';
return '/'.ltrim(
str_replace(
'?'.$query,
'',
str_replace(
rtrim(
str_replace(
last(explode('/', $_SERVER['PHP_SELF'])),
'',
$_SERVER['SCRIPT_NAME']
),
'/'),
'',
$_SERVER['REQUEST_URI']
)
),
'/');
}
}
Then, in you bootstrap/app.php
, change this:
/*
|------------------------
| Create The Application
|------------------------
|
| Here we will load the environment and create the application instance
| that serves as the central piece of this framework. We'll use this
| application as an "IoC" container and router for this framework.
|
*/
$app = new App\Application(
realpath(__DIR__.'/../')
);
After this, you don't need to change public/index.php
file, just let it default:
/*
|---------------------
| Run The Application
|---------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/
$app->run();