Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 880
Musterknabe Avatar asked May 28 '15 16:05

Musterknabe


People also ask

Are there any real world examples of Laravel basepath?

PHP Laravel\Lumen Application::basePath - 2 examples found. These are the top rated real world PHP examples of Laravel\Lumen\Application::basePath extracted from open source projects. You can rate examples to help us improve the quality of examples.

What is the difference between Lumen and Laravel?

Lumen is micro-framework based on laravel, lumen have same foundation as laravel and also have many same component. But it is faster and leaner than the full application. Of course, i used Laravel for my major project but prefer lumen and slim framework for my api project that need to be faster.

What is a facade in Laravel?

In a Laravel application, a facade is a class that provides access to an object from the container. The machinery that makes this work is in the Facade class. Laravel's facades, and any custom facades you create, will extend the base Illuminate\Support\Facades\Facade class.

How to generate HTTP URL in Laravel 4?

What you want to use is the path helper. The URL helper is for generating HTTP URLs. or the public_path () function in Laravel 4. Show activity on this post.


1 Answers

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();
like image 60
krisanalfa Avatar answered Oct 11 '22 19:10

krisanalfa