Hello so I am using slim framework for a simple project and currently having an issue. My code for throwing a 404 error is:
$app->notFound(function () use ($app) {
$app->render('404.html');
});
The code will go to the 404 page for 2 unknown url parameters like website.com/1st/2nd/ but when there are 3 or more like website.com/1st/2nd/3rd/, the code still works but it doesn't carry out anymore the assets(css/js).
My route configs is this:
require_once 'vendor/autoload.php';
$app = new \Slim\Slim([
'debug' => true,
'templates.path' => 'app/views'
]);
$app->view = new \Slim\Views\Twig();
$app->view->setTemplatesDirectory("app/views");
$view = $app->view();
$view->parserOptions = ['debug' => true];
$view->parserExtensions = [new \Slim\Views\TwigExtension()];
I am using twig in my views. I followed this tutorial. My folder structure looks like this:

In my base template, this is how I include my assets(css/js).

It only shows the 404 page text. Is there any problem here?
Perhaps you should use base href as the css and scripts sources are usually relative.
Use the
baseUrl()
once in your "head" tag like this;
<head>
<base href="{{ baseUrl() }}">
</head>
You can then use relative asset urls like this;
<img src="/assets/img/cover.png" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With