Cannot get css or js to load in slim 4

I am using Odans slim-4-skeleton-master, but instead of php-view I am using Twig. I followed his tutorial on loading twig and everything works except retrieving css or js from public folder. I am not using assets or webpack, just css and js inside the public folder. When I dump {{ url_for(‘login’) }} I get ‘/’. When I dump {{ base_path() }} ,{{ base_path }}, or {{ base_url }} i get nothing. The error in dev tools is this http://localhost:8081/css/login.css net::ERR_ABORTED 500 (Internal Server Error). I cannot figure out how to get to my css or js. I’ve been trying all kinds of things all day. I am curious how to get <link rel="stylesheet" type="text/css" href="{{ base_path }}/css/login.css"> to work. I am also using selective/basepath. I’m not sure if that has anything to do with it.

Hi @meazy

Please take a look at the documentation of the Slim/Twig package. The base_path() is a Twig function that returns the Slim application basePath. When you set the basePath with $app->setBasePath('/sub-dir')'; then base_path() should return /sub-dir. The selective/basepath just detects the sub-directory (if any) and passes the detected basePath to the Slim $app instance automatically.

If {{ base_path() }} returns nothing, then you (possibly) already run your application directly in the DocumentRoot of your web server. Please note that the selective/basepath middleware only really supports Apache and the PHP internal web server. Other web servers like nginx may not work correctly because PHP-FPM works differently.

For a quick troubleshooting, I provide a Slim framework Server Configuration Checker script that gives you more details about the configuration. It would be nice if you could post the result here.

I am running on a the php built in server. I have always done php -S localhost:8080 -t public public/index.php. For slim 4 i have to cd into the public directory and do php -S localhost:8888. I didn’t change anything else. So now I’m wondering, will this break when i put it on a real server? Is there a cloud service that has a slim container to test in? Like Symfony cloud.

Ok, then you should not have any problems with the basePath, because your DocumentRoot points already to the public/ directory. On a production server, this should also be configured in the same way.

Is there a cloud service that has a slim container to test in? Like Symfony cloud.

No, as far as I know. You can still deploy to your QA or staging server and see if everything is configured correctly.