Webroot index.php relation to the complete application

*I wanted to create a homepage and assumed the webroot index.php is appropriate. I found out it is interrelated with other templates, such that e.g. <h3>Root index Code</h3> will appear at the top of all templates and the page is needed to bind and run the actual app. *

So far I am only used to work with index methods and would expect to make an index method in the appController as all index URLs have in their corresponding Controllers. This first argument is however expected to be a controller (such that root/index expects an indexController).

EDIT: I found the actual home.php file. Do “Pages” belong to the appController, or are they outside of the MVC? I can’t find this in the documentation. This topic can be removed then, although it would be insightful to understand why the webroot index.php is called ‘index’ .

webroot/index.php is not a place that you should be putting any HTML. The “index” part of that filename is completely unrelated to the index functions that you have in your controllers.

If you have something you want to show up in all pages, that’s a layout, look at src/Template/Layout/default.ctp (in Cake 3) or templates/layout/default.ctp (in Cake 4).

You also don’t want to be putting an index function in the AppController. That class is for providing functionality that is common to all of your controllers. They may all have index functions, but those functions may all be a bit different, so the implementation of that wouldn’t generally go into your AppController. More common use of the AppController is for things like a beforeFilter or beforeRender callback, for example.

As for pages, those are handled by the PagesController that comes with the default app. In your config/routes.php, there’s a route that maps the ‘/’ URL to ['controller' => 'Pages', 'action' => 'display', 'home'], and that’s where home.php comes in.

Not sure what you’re thinking of when you mention root/index; you mean a URL like https://example.com/index? That’s not really something people do, not in my experience, anyway. You just use https://example.com/. If you really wanted and needed to have https://example.com/index, then you either make an IndexController, or you add a route that maps that URL to some other controller and action. Maybe ['controller' => 'Pages', 'action' => 'display', 'index'], for example, and then you put an index.php in the Pages template folder (again, completely unrelated to the index.php under webroot).

1 Like