Basic Concepts
This topic collects the concepts you will use every day: the front controller, routes, controllers, views, models, helpers, libraries, services, filters, and namespaces.
Routes
Routes map URLs to controller methods. They live in app/Config/Routes.php and are evaluated from top to bottom.
$routes->get('/', 'Home::index');
$routes->get('products/(:num)', 'Catalog::show/$1');
$routes->post('contact', 'Contact::submit');
$routes->group('admin', ['filter' => 'auth'], static function ($routes) {
$routes->get('dashboard', 'Admin\Dashboard::index');
});
Controllers
A controller receives the request and returns a response. Keep it thin: validation and flow belong here, while data rules live in models and reusable work lives in services.
namespace App\Controllers;
class Pages extends BaseController
{
public function about()
{
return view('pages/about', ['title' => 'About']);
}
}
Views
Views are plain PHP templates in app/Views. Pass data from the controller and escape output with esc().
return view('pages/about', ['title' => 'About']);
Models
Models handle table access and data rules. A model can use the built-in create, read, update, delete helpers or the query builder.
Helpers, libraries, and services
- Helpers are collections of small functions, such as
base_url()oresc(). - Libraries are reusable classes, such as an invoice calculator.
- Services create shared objects such as the request, response, session, or logger.
Filters
Filters run before or after controllers. Authentication, rate limits, and response headers are common examples. Apply them to route groups so the checks are not repeated in every method.
Conventions worth following
- Controllers and models use PascalCase file names; methods use camelCase.
- Database tables use lowercase snake_case names.
- Keep configuration in
app/Configand environment values in.env. - Use the
Appnamespace and match folders to namespaces. - Only
publicis web-accessible.