Skip to content
Advertisement

TwigLoaderFilesystemLoader not found when triggering 404 error

I’m new to OOP and MVC with PHP, and I’m currently learning by making my own custom MVC app for testing purposes. I’m not using Symfony yet, but I’ve integrated Twig and I have a problem with it when I call a 404 error outside a controller, in the two following cases :

  • When requested page doesn’t exist on server (like http://localhost/test/)
  • When requested URL doesn’t match with an existing controller and method (like http://localhost/test/task/)

I’ve got the following error :

JavaScript

However, if I call the right controller with an existing method, everything works fine (like http://localhost/article/show/123). If I call 404 error inside a controller method (e.g. if the $_GET ID of an article does not exist in the DB), everything works fine too and my 404 error template is correctly rendered.

How I call a 404 error ?

I use a static method Http::error404() that render my 404 error Twig template.

JavaScript

Application class

My App use a mini-router named Application. It checks if the controller and the called method exists. If not, call 404 error with Http::error404(), and it is in this case that the above fatal error appears.

JavaScript

There is a strange thing here : if I define any controller before calling 404 error, everything works as I would like and my 404 error template is correctly rendered. But it’s not a clean and elegant solution…

JavaScript

The same thing happens in my 404.php file, called by the server and declared in the .htaccess when a file is called and it doesn’t exist. My 404 PHP file just contains few lines :

JavaScript

If I define any controller, it’s working perfectly and fatal error disappears.

JavaScript

Render Class

This is my rendering class. The fatal error appears in this file, as shown in line 32 below, when Http class with error404 method render errors/404 Twig template.

JavaScript

I have only added the code that I think is related to my issue. Do not hesitate to ask if there are any points that need to be clarified.

I’ve been looking for a solution to this error for seven days and haven’t found a solution. Your help is my last resort. Thank you very much!

Advertisement

Answer

I removed all the many unnecessary require_once autoloader in app files, and kept only the one in index.php, as indicated by @NicoHaase.

From that point, Twig is no longer found and the same error message appears on all requests !

The solution was easy to find : in my index.php file, I was using the wrong autoloader. I was calling my custom autoloader instead of the one from Composer…

I just change :

JavaScript

To :

JavaScript

And everything works fine now ! Thanks to @NicoHaase and @DarkBee for putting me on the right track !

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement