After this lesson you can locate application code, public files, installed dependencies and runtime data without editing the wrong directory. Open the disposable bookshop created previously. This is a reading exercise: understanding ownership of files now prevents accidental exposure and fragile fixes later.
Separate the project root from the web root
The project root contains the whole application. The public directory is the intended document root for the web server. These are different boundaries. Visitors need access to the front controller and deliberate public assets; they should not browse application configuration, tests, package metadata or runtime database files.
bookshop/
app/
Config/
Controllers/
Database/
Filters/
Models/
Views/
public/
index.php
tests/
vendor/
writable/
composer.json
composer.lock
sparkThis is a selected directory map observed in the installed starter, not output from a tree command. Some subdirectories are omitted to keep the responsibilities visible. CodeIgniter's official Application Structure guide explains these boundaries. The bookshop adds its own model, migration, controller and view files within the existing structure.
A request enters through public/index.php. Framework bootstrapping then loads the application and its configuration. You do not need to paste a database connection into that entry file. Its job is to start the application, not to become a second location for business behavior.
Put each change where its owner lives
Routes belong in app/Config/Routes.php. Controllers belong in app/Controllers. A class named App\Controllers\Books is therefore expected in app/Controllers/Books.php under the starter's namespace mapping. Namespace names, class names and filename capitalization should agree. A forgiving development filesystem can conceal a mismatch that later fails on a case-sensitive server.
Views are PHP templates under app/Views. Calling view with the name books/index points to app/Views/books/index.php. The view name is an application-relative template name, not a public URL. A visitor cannot request a template directly when the server's document root is correctly set to public.
Models live under app/Models. Database migrations and seeders are under app/Database. A migration changes a schema; a seeder creates known data. Keeping these distinct lets you change table structure without accidentally inserting duplicate demonstration books on every request.
Filters belong under app/Filters. They provide a place for request-wide checks or policy boundaries around selected routes. Their existence in a directory does not activate them. A later lesson attaches a filter to a route and verifies that the request stops before the controller.
Know which files are generated or installed
Composer owns vendor. The framework implementation is installed under vendor/codeigniter4/framework. You may read it to understand a method, but course changes go into app. If a framework behavior needs configuration, find the documented application setting instead of patching the installed package.
The writable directory holds runtime files. Our project later puts a disposable SQLite database there. Logs, caches, sessions and uploads may also live below writable according to configuration. These files need suitable filesystem permissions for the runtime account, but granting every user write access is not a sound default. Permissions depend on your hosting arrangement and should be reviewed there.
Tests belong under tests. They exercise application behavior without becoming browser endpoints. A test file is not a substitute for a controller, and a controller used only for temporary diagnostics should not remain exposed accidentally. Keep exploratory checks separate from the route map you intend to deliver.
Trace a proposed change before making it
Suppose you want a book detail page. The route file will map a numeric path to a controller method. The controller will ask a model for data. A view will render the result. The table itself will be created by a migration. You can now identify four files with four separate purposes before writing any of them.
This mapping is a practice for reducing confusion, not a rule that every request must touch every layer. A health response can be a controller response without a database. A static asset can be served directly from public. Choose components because the responsibility needs them, not because the folder exists.
The fetched official Application Structure guide is the reference for directory roles. In this course, the fictional shop's data remains outside public and no repository environment file is opened. Follow the same isolation when trying examples alongside an existing project.
Common mistakes
Using the project root as the document root exposes more files than intended. Editing vendor creates changes that dependency installation may remove. Putting uploads directly under public without a deliberate serving policy mixes storage with publication. Another common error is passing a filesystem path such as app/Views/books/index.php as if it were a browser route. Paths, class names and URLs are related by configuration, not interchangeable strings.
Exercise
Place these responsibilities in directories: route declaration, book title template, schema creation, request filter and database log. Explain which directory a web server should serve and why the SQLite database must not be put there.
Show an answer
The route goes in app/Config, the template in app/Views, the migration in app/Database/Migrations and the filter in app/Filters. Runtime logs belong in the configured writable log area. The server serves public. A SQLite file contains application data and belongs outside that public boundary, even when its current contents are fictional.
Previous lesson · Next lesson · Related: explicit routes · Related: security boundaries
Key points
- Serve public rather than the complete project root.
- Edit application classes under app, not installed framework classes under vendor.
- Keep runtime data outside the public document root.