Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slim Framework 404 css issue

Tags:

css

php

twig

slim

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:

enter image description here

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

enter image description here

It only shows the 404 page text. Is there any problem here?

like image 493
wobsoriano Avatar asked Dec 01 '25 18:12

wobsoriano


2 Answers

Perhaps you should use base href as the css and scripts sources are usually relative.

like image 123
Jan Avatar answered Dec 04 '25 06:12

Jan


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" />
like image 26
Ugokoli Avatar answered Dec 04 '25 06:12

Ugokoli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!