The app/routes.php file is the map of your web application: it decides which controller handles which URL.
Every path of your website starts with a route function in routes.php. The function name is the first part of the URL,
and the function hands the request to a controller with View::route(). FrostMVC reads the URL like this:
https://<domain.tld>/[routeFunction]/[controllerMethod]/[param-1]/[param-2]/.../[param-N]
routes.php.index() runs.index() route function.
Route functions live in the <YourNamespace>\routes namespace, where the namespace is the $config['namespace'] value of
settings.php. Import each controller with use and route it by its class name:
<?php
namespace MyApp\routes;
use FrostMVC\View;
use MyApp\controllers\BlogController;
use MyApp\controllers\HomepageController;
function index() {
View::route(HomepageController::class);
}
function blog($pages) {
View::route(BlogController::class, $pages);
}
$pages and pass it on. It holds the rest of the URL, which FrostMVC uses to pick the controller method and its arguments.View::route() call. Application logic belongs in the controller.View::route('BlogController', $pages)) from a file without a namespace.
Add namespace MyApp\routes;, import the controllers and use BlogController::class.
With the blog() route above and this controller:
class BlogController extends Controller {
public function index() { /* list posts */ }
public function latest() { /* newest posts */ }
public function view($id, $slug = '') { /* one post */ }
}
| URL | Runs |
|---|---|
| https://www.mysite.com/ | index() route โ HomepageController::index() |
| https://www.mysite.com/blog | BlogController::index() |
| https://www.mysite.com/blog/latest | BlogController::latest() |
| https://www.mysite.com/blog/view/1145/frostmvc-routing | BlogController::view('1145', 'frostmvc-routing') |
| https://www.mysite.com/blog/view/1145 | BlogController::view('1145') โ $slug keeps its default |
| https://www.mysite.com/blog/view | Error: view() needs at least one argument |
| https://www.mysite.com/blog/missing | Error: method missing() not found |
| https://www.mysite.com/unknown | Error: route unknown() not found |
In the Development environment, errors name the missing route or method to help you debug; in Production visitors see a generic page instead.
Development also enables the built-in /phpinfo route.
Query strings are not part of the route. Read them in the controller with Form::fig() (GET), Form::fip() (POST) or Form::val() (GET, then POST):
// https://www.mysite.com/blog/latest?page=2
public function latest() {
$page = Form::fig('page', FILTER_VALIDATE_INT) ?: 1;
}
Next: Controllers ยท Reference: View, Controller