-1

I am new to Laravel and can't seem to find the answer to a technical detail of the implementation. Essentially, when I put any given file inside the public directory, for e.g. app.css, and then navigate to localhost:8000/app.css, the file gets served without me having to do anything.

This obviously leaves me wondering about how Laravel handles the request to localhost:8000/app.css. To test things out, I tried adding a GET route inside web.php as follows:

Route::get('/app.css', function() {
   return 'Hello';
});

but when I navigate to localhost:8000/app.css, it still returns the app.css CSS file, not the 'Hello' text. This clearly means that Laravel first searches for a file inside the public directory and if it can't find one only then does it bring in the router.

My question is: Is this something that Laravel does or is it something that is done intrinsically by the built-in PHP server? In simpler words, is returning static files inside public a concern of the server itself or of Laravel.

I tried inspecting the source code of Laravel but unfortunately I can't trivially find any file where a check is made for a matching file in public before serving it.

2
  • 1
    If the webroot/htaccess is set up to point to the public directory, then pretty much everything in the public directory bypasses the index.php, which loads Laravel. This would be done by the web server (Apache, nginx, php serve) Commented Jun 4, 2024 at 17:03
  • Just look at the .htaccess. Laravel only handles requests to files whch don't exist on disk, anything else is ignored and simply handled by your web server. Commented Jun 5, 2024 at 4:22

2 Answers 2

0

This is server related of course. Apache and Nginx are widely used web servers. Whichever you use, you can determine whether to redirect an incoming request to an index.php or a static file.

An example conf for Nginx might be:

server {
listen 80;
listen [::]:80;

root /home/www/test.com/public;

index index.php;

server_name www.test.com;


location ~ [^/]\.css(/|$) {
    try_files $uri $uri/ /index.php;
}

error_page 404 /index.php;

location ~ \.php$ {
            add_header X-Is-PHP true;
            #try_files $uri =404;
            try_files /index.php =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            # With php5-fpm:
            fastcgi_pass unix:/var/run/php8.2-fpm.sock;
            fastcgi_index index.php;
            include fastcgi.conf;
    }
}

This sample code makes index.php interpret all css files. Even though this is not the correct method, I added it to explain how it works.

I think the correct method should be what you encounter. In other words, if the file is physically located on the disk, it must be returned, otherwise index.php must interpret it. For this reason, if you want Laravel to open the file instead of index.php, you should delete that file.

Sign up to request clarification or add additional context in comments.

Comments

0

If you are using php artisan serve, Laravel checks for the existence of files in public.

// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' && file_exists($publicPath.$uri)) {
    return false;
}

https://github.com/laravel/framework/blob/11.x/src/Illuminate/Foundation/resources/server.php

This is a common behavior on web servers.

1 Comment

Built-in servers are for development only, they should not be used for production.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.