简体   繁体   中英

What is the LOCAL path to a public asset in Laravel 5? (using server.php)

I'm using Laravel for the first time, and running into problems with my localhost server.

Once I deploy the site, I can view an image I uploaded at the following address:

www.mysite.com/storage/img/1.jpg

mysite.com is set to Laravels public folder. Visiting the mysite.com url displays the laravel welcome page.

Now for localhost, which runs using the following command, thus running Laravels server script:

php -S localhost:4000 server.php

Visiting the localhost:4000 url displays the laravel welcome page. Appending this url with my named routes runs Laravel routes as expected. But I CANNOT access the .jpg I mentioned at any address! Even when referenced within a laravel view.

http://localhost:4000/storage/img/1.jpg

^returns a 404 not found page

http://localhost:4000/public/storage/img/1.jpg

^returns the laravel welcome page again!

How do I reference assets, or indeed anything within my public folder, while using the server.php script to set up a local environment? These paths do not work either absolute or relative from within a view. Thanks in advance!

My (unedited) server.php script looks like this:)

 <?php

/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <taylorotwell@gmail.com>
 */

$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';

You are doing it wrong, because in Laravel 5 the storage for blades is in resources and there you will find also the assets folder inside of it the images folder. I advice you to use this structure. Also please don't user php -S for server, instead use an environment like mamp , wamp or xampp as server. You will have permission issues and ownership issues. Hope it helps.

From http://laravel.com/docs/master/structure

The storage directory contains compiled Blade templates, file based sessions, file caches, and other files generated by the framework

Maybe your {app}/storage folder is having some sort of conflict with your {app}/public/storage folder? Consider using another name for "storing" uploaded file?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM