Making use of the base path in Laravel 5 (Lumen)

老子叫甜甜 提交于 2019-12-05 06:58:05

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.

Additional

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

tested with Lumen 5.4 / apache / FPM/FastCGI:

folder structure:

/api
├── index.php
├── .htaccess
/lumen-api/
├── app
├── bootstrap
..

Web Root: / URL: http://www.domain.dev/api

copy Files from directory /lumen-api/public to /api

change /api/index.php:

1) adapt path to directory with lumen bootstrap

$app = require __DIR__.'/../lumen-api/bootstrap/app.php';

2) fix wrong baseURL add this after "$app = require __DIR_...." to fix wrong baseURL of /vendor/symfony/http-foundation/Request.php:protected function prepareBaseUrl()

$_SERVER['SCRIPT_NAME']='index.php';

3) sample /lumen-api/routes/web.php:

$app->get('/api/', ['as' => 'home',function () use ($app) {
    return $app->version() . " - " . route('home');
}]);

4) Test http://www.domain.dev/api Result should be:

Lumen (5.4.0) (Laravel Components 5.4.*) - http://www.domain.dev/api

if you get http://www.domain.dev/api/api two times /api/api - the fix for the baseURL from 2) is not working!

In my case I moved .htaccess from public directory to root directory and created an index.php file in my project root and its looks like this. If you have a laravel project you can copy its server.php from root and change its name to index.php

index.php

<?php

$uri = urldecode(
    parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);

if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
    return false;
}

require_once __DIR__.'/public/index.php';
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!