Mooncake
Mooncake

Reputation: 1553

Specify a different public path

My Laravel app works in the private folder and I need to tell Laravel that the public path is different. Today I have upgraded a Laravel app from 4.2 to 5.0 and I can't find where we specify the public path since the paths.php file doesn't exist anymore in Laravel 5.0.

In laravel 4.2 we had the /bootstrap/paths.php file:

/*
|--------------------------------------------------------------------------
| Public Path
|--------------------------------------------------------------------------
|
| The public path contains the assets for your web application, such as
| your JavaScript and CSS files, and also contains the primary entry
| point for web requests into these applications from the outside.
|
*/

'public' => __DIR__.'/../../../public_html',

I'm not used with the Laravel 5.0 folders structure yet, any help would be greatly appreciated.

Upvotes: 5

Views: 10237

Answers (3)

manix
manix

Reputation: 14747

I think that this could be made in different ways, here is mine.

Create a new helper file. You can create it at Services folder:

# app/Services/helper.php

if ( ! function_exists('private_path')){
    function private_path($path = ''){
       return app_path() . 'private/'
    }
}

A good place to import the helper file is in AppServiceProvider that resides at app/Providers/AppServiceProvider.php. Use the boot to do so.

public function boot()
{
    include __dir__ . "/../Services/helper.php";
}

Rename the folder from public to private, and finally you can call your own function from anywhere as:

$path = private_path();

Upvotes: 2

Mooncake
Mooncake

Reputation: 1553

According to this post, to replace the original public path we need to override the application paths:

<?php namespace App;

use Illuminate\Foundation\Application;

class MyApp extends Application {

    protected $appPaths = array();

    /**
     * Create a new Illuminate application instance.
     *
     * @param  array|null $appPaths
     * @return \MyApp
     */
    public function __construct($appPaths = null)
    {
        $this->registerBaseBindings();

        $this->registerBaseServiceProviders();

        $this->registerCoreContainerAliases();

        if (!is_array($appPaths)) {
            abort(500, '_construct requires paths array');
        }

        if (!isset($appPaths['base'])) {
            abort(500, '_construct requires base path');
        }

        $this->appPaths = $appPaths;

        $this->setBasePath($appPaths['base']);
    }

    /**
     * Set the base path for the application.
     *
     * @param  string  $basePath
     * @return $this
     */
    public function setBasePath($basePath)
    {
        $this->basePath = $basePath;

        $this->bindPathsInContainer();

        return $this;
    }

    /**
     * Bind all of the application paths in the container.
     *
     * @return void
     */
    protected function bindPathsInContainer()
    {
        $this->instance('path', $this->path());

        foreach (['base', 'config', 'database', 'lang', 'public', 'storage'] as $path)
        {
            $this->instance('path.'.$path, $this->{$path.'Path'}());
        }
    }

    /**
     * Get the path to the application "app" directory.
     *
     * @return string
     */
    public function path()
    {
        return $this->basePath.'/app';
    }

    /**
     * Get the base path of the Laravel installation.
     *
     * @return string
     */
    public function basePath()
    {
        return $this->basePath;
    }

    /**
     * Get the path to the application configuration files.
     *
     * @return string
     */
    public function configPath()
    {
        if (isset($this->appPaths['config'])) {
            return $this->appPaths['config'];
        }
        return $this->basePath.'/config';
    }

    /**
     * Get the path to the database directory.
     *
     * @return string
     */
    public function databasePath()
    {
        if (isset($this->appPaths['database'])) {
            return $this->appPaths['database'];
        }
        return $this->basePath.'/database';
    }

    /**
     * Get the path to the language files.
     *
     * @return string
     */
    public function langPath()
    {
        if (isset($this->appPaths['lang'])) {
            return $this->appPaths['lang'];
        }
        return $this->basePath.'/resources/lang';
    }

    /**
     * Get the path to the public / web directory.
     *
     * @return string
     */
    public function publicPath()
    {
        if (isset($this->appPaths['public'])) {
            return $this->appPaths['public'];
        }
        return $this->basePath.'/public';
    }

    /**
     * Get the path to the storage directory.
     *
     * @return string
     */
    public function storagePath()
    {
        if (isset($this->appPaths['storage'])) {
            return $this->appPaths['storage'];
        }
        return $this->basePath.'/storage';
    }
}

This seems weird for me and as it's been mentioned in the post, it feels like we did a step back in Laravel's functionalities, I hope they will change it in a future update.

Upvotes: 0

Rubens Mariuzzo
Rubens Mariuzzo

Reputation: 29231

What worked for me flawlessly was adding to public/index.php the following three lines:

$app->bind('path.public', function() {
    return __DIR__;
});

This was answered at Laracast.

Upvotes: 13

Related Questions