Defining Routes

Routing is a mapping mechanism that allows controller actions to be bound to the structure of the url. Since controller actions are by definition urls, the default structure is Example/articles/?name=Framework Example&month=November&year=2015.

If a route is defined for the above example, we would simply have a url in the following format: Example/articles/Framework Example/November/2015

Below the ExampleController has been shortened for the purposes of this example.

<?php
    class ExampleController extends FrontController
    {
        /**
         * Initialization
         *
         */
        public function initialize()
        {
            $this->addRoute("articles");
        }

        /**
         * URL: Example/articles/?name=&year=&month=
         * Route: Example/articles/name/year/month
         * Alias: my-articles/
         *
         * @param string             $name a dummy holder for name of fake article
         * @param string             $year a dummy holder for year of fake article
         * @param string             $month a dummy holder for month of fake article
         * @return View
         */
        public function articlesAction($name = "", $year = "", $month = "")
        {
            $this->view->title = "Articles Route Test";
            $this->view->content = "Hello World from the <b>ExampleController articlesAction</b> action<br /><br />";

            $this->view->myBoundParameters = array(
                $name, $year, $month
            );

            return $this->view();
        }
    } /*end of class ExampleController*/

The convention is to define a route inside the initialize function of the Controller. Simply call the $this->addRoute(""); method which accepts the name of an action. The other convention to add a route can be seen inside the automatic routing controller example.

Additional Use Case

Routing can be used to help make urls more human readable (and SEO friendly). When controller actions have been defined as routes the mapping to the parameters is more dynamic, instead of being as rigid as "normal" controller actions. What happens in cases where routes have no parameters, or has more, or has less values provided than is needed? Here is a list of urls:

  1. Example/blog/
  2. Example/blog/November/2015/how-routing-can-be-dynamic/1432454
  3. Example/blog/another-dynamic-url/no-parameters/still-valid/

Naturally this use case would need to have logic built around it to manually parse the individual parts of the url.

Below the ExampleController has been shortened for the purposes of this example.

<?php
    class ExampleController extends FrontController
    {
        /**
         * Initialization
         *
         */
        public function initialize()
        {
            $this->addRoute("blog");
        }

        /**
         * URL: Example/blog/
         * Route: Example/blog/
         *
         * @return View
         */
        public function blogAction()
        {
            $this->view->title = "Blog Entry";

            return $this->view();
        }
    } /*end of class ExampleController*/

Below is the content of the application/views/templates/Example/blog.php view template.

<h2><?php echo $this->title;?></h2>

<?php
echo FrontController::url();

One More Thing

Did you know that that aliases also respect any routing and automatic parameter binding attached to a controller?
The my-articles/Framework Example/November/2015 alias points to the same controller action from the first example shown above.