Routes

Routes are classes which are triggered when a specific url is visited. They help to control the response obtained or activity performed at the trigger of such classes. Routes are also window files which means that they must be extended to the root Window class either directly or by the use of Frames. Whenever Routes are extended to Frames, it is usually because they want to inherit a particular property, data or functionality of that specific Frame which is also a window class. Route files are usually placed within the window/Routes directory.

Routes Example

Home.php (sample window)
  <?php

  namespace spoova\mi\window\Routes;

  use Window;

  class Home extends Window {

    function __construct(){
        
        echo "This is some route";

    }
    
  }

                          

Route Methods
Route methods are designed to control or obtain information about declared route names. These route methods are addRoutes(), loadRoutes() and getRoutes().

addRoutes
This method is used to declare names for routes on particular Route controller class which can later be loaded using the loadRoutes() method. It follows a predefined structure in which when an array of key pairs "name" and "route" is supplied, then such route will be assigned the specific name. Examples are shown below:

sample 1 - Local addRoutes()
  <?php

  namespace spoova\mi\window\Routes;

  use Window;

  class Home extends Window {

    function __construct(){

    }

    function addRoutes(array $array = []) : array {

        return [
        
          'profile' => 'home/profile' //set name "profile" for "home/profile" route

        ];
    }

  }

                              
In the sample above, the addRoutes() method ensures that a name is assigned for the particular route home/profile. Once the name of the route is declared, the name can later be used to access the specified route when it is updated. We can define routes names globally for a route controller using the syntax below:
sample 2 - Global addRoutes()
  <?php

  namespace spoova\mi\window\Routes;

  use Window;

  class Home extends Window {

    function __construct(){

    }

    function addRoutes(array $array = []) : array {

        return Window::addRoutes([
        
          'profile' => 'home/profile' //set name "profile" for "home/profile" globally

        ]);
    }

  }

                              
While sample 1 and 2 above may look similar, there is a slight difference. The major differences is that in sample 1 above where the addRoutes() only returns an array, route names declared usually stay unknown to the class itself unless a method loadRoutes() is used within the constructor function. However, in sample 2 where the Window::addRoutes() is used, the declared names are automatically processed and accessible within the class. Using sample 2 above as reference, we can access the assigned name of home/profile route by using the function routes('profile') or directive @routes('profile') in template files. In order to achieve the same effect in sample 1, the loadRoutes() method must first be applied.

loadRoutes

By default, the loadRoutes() method calls the addRoutes() method of the root Window class or last globally named route. However, since addRoutes() method can be re-defined in child Window classes, if the addRoutes() is re-defined, then only the root Window::addRoutes() method, can be used to update the named urls. This example is displayed in sample 2 of addRoutes discussed earlier. In the event that the addRoutes() method of the current Route uses a direct array only, then it is required to pass the current Window class as an argument into the loadRoutes() method when calling it within that class. A sample of this is shown below.


  use Window;

  class Home extends Window {

    function __construct(){
        
        self::loadRoutes($this); //load routes
        
        echo routes('profile'); # home/profile

    }

    function addRoutes(array $array = []) : array {

        return [
        
          'profile' => 'home/profile' 

        ];
    }

  } 

                        
In the sample above, the __construct() method was used to update the default routes by passing the instance of the current class into the loadRoutes() method which by default automatically updates the routes by using the root Window::addRoutes() to pull the route names from the addRoutes() method of the current class. Once this is done, then the routes() function and @route() template directive will be able to access the declared names.

getRoutes

This method only returns the list of named routes for a particular window class. Before this method can successfully return the list of named route of a class, the loadRoutes() or Window::addRoutes() method must have been used. If these methods are not applied, then the default named routes are returned, if any.

Sample 1 - getRoutes()

  use Window;

  class Home extends Window {

    function __construct(){
        
        self::loadRoutes(this);

        vdump(self::getRoutes()); # ['profile' => 'home/profile']      

    }

    function addRoutes(array $array = []) : array {

        return [
        
          'profile' => 'home/profile' 

        ];
    }

  } 
                        


Sample 2 - getRoutes()

  use Window;

  class Home extends Window {

    function __construct(){

        vdump(self::getRoutes()); # ['profile' => 'home/profile']    

    }

    function addRoutes(array $array = []) : array {

        return Window::addRoutes([
        
          'profile' => 'home/profile' 

        ]);
    }

  }