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.
<?php namespace spoova\mi\window\Routes; use Window; class Home extends Window { function __construct(){ echo "This is some route"; } }
addRoutes()
, loadRoutes()
and getRoutes()
.
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:
<?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
];
}
}
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:
<?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
]);
}
}
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()
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' ]; } }
__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.
loadRoutes()
or Window::addRoutes()
method must have been used.
If these methods are not applied, then the default named routes are returned, if any.
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'
];
}
}
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'
]);
}
}