Welcome to FrostSW!

FrostMVC PHP Framework

Documentation   |   Version
FrostMVC / app / routes.php

routes.php

The app/routes.php file is the map of your web application: it decides which controller handles which URL.

How routing works

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]
  • The first part (right after the domain) is the name of a function in routes.php.
  • The second part is the name of a public method of the routed controller. When it is missing, index() runs.
  • The remaining parts are passed, in order, as the arguments of that method.
  • A URL with no path at all (your homepage) runs the index() route function.

The routes file

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);
}
  • The function name becomes the path, so it may only contain letters, numbers and underscores.
  • Always accept $pages and pass it on. It holds the rest of the URL, which FrostMVC uses to pick the controller method and its arguments.
  • Keep route functions to a single View::route() call. Application logic belongs in the controller.
Upgrading from FrostMVC 1.0? Routes used to pass the controller as a string (View::route('BlogController', $pages)) from a file without a namespace. Add namespace MyApp\routes;, import the controllers and use BlogController::class.

Examples

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 */ }
}
URLRuns
https://www.mysite.com/index() route โ†’ HomepageController::index()
https://www.mysite.com/blogBlogController::index()
https://www.mysite.com/blog/latestBlogController::latest()
https://www.mysite.com/blog/view/1145/frostmvc-routingBlogController::view('1145', 'frostmvc-routing')
https://www.mysite.com/blog/view/1145BlogController::view('1145') โ€” $slug keeps its default
https://www.mysite.com/blog/viewError: view() needs at least one argument
https://www.mysite.com/blog/missingError: method missing() not found
https://www.mysite.com/unknownError: route unknown() not found
Only public controller methods can be reached from a URL, and only when the URL supplies at least as many values as the method has required parameters. Use optional parameters for values that may be left out.

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

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