phplaravellaravel-4.2thephpleague

Route not triggering in Laravel 4.2


I have a folder with images in it at example.com/img/, and at the same time I have a route

get('img/{path}', 'ImageController@output');

But when I try going to example.com/img/someimagepath and if image name is someimagepath I get that image instead of firing route above.

Is there a way to make it fire route instead of requesting a file?

To be precise, I'm using https://github.com/thephpleague/glide Glide library


Solution

  • Its because the server is looking in the public folder, finding the img directory, and serving the image from there instead of loading public/index.php. If index.php is not loaded, laravel is not loaded, so the routes won't work.

    Either rename the folder in your public directory, or rename your route. You cannot have them both referencing img

    I'd keep the directory the same, and rename your route:

    get('images/{path}', 'ImageController@output');
    

    or maybe prefix your routes if you really like img as a segment:

    get('app/img/{path}', 'ImageController@output');
    

    Basically, your routes cannot follow any path hierachry in public. The way the sever knows to load laravel is if it cannot find a directory or file in public that matches the URI, it loads index.php. If it does find a directory or file path matching the URI, it will serve that instead.

    From public\.htaccess. Basically saying if the request does not match a file or directory, load index.php

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]