Default routing is based on typical MVC structure in which URI corresponds to class/method.
http://example.com/blog/list/param1/param2
In this example the request will be routed to public
list
method of blogController
with param1 & param2 as method arguments. A sample controller would look something like this.
namespace sampleSite\controller
{
use hathoora\controller\controller;
/**
* Blog controller
*/
class blogController extends controller
{
/**
* List action
*/
public function list($param1, $param2, $param3 = null)
{
//
}
}
}
Index
page or homepage of an application is routed to defaultController
.
http://example.com/
In this example the request will be routed to public
index
method of defaultController
. A sample controller would look something like the following.
namespace sampleSite\controller
{
use hathoora\controller\controller;
/**
* Homepage controller
*/
class defaultController extends controller
{
/**
* homepage action
*/
public function index()
{
//
}
}
}
If you don't specity an action for a controller, then public
index
method of that controller is called.
http://example.com/blog/
In this example the request will be routed to public
index
method of blogController
as shown below.
namespace sampleSite\controller
{
use hathoora\controller\controller;
/**
* Blog controller
*/
class blogController extends controller
{
/**
* home action of blog
*/
public function index()
{
//
}
}
}
Camel case method names are seperated by a dash (-). Consider the following URLs which are both pointing to the same action.
http://example.com/blog/orderBy/name
http://example.com/blog/order-by/name
In this example both requests will be routed to public
orderBy
method of blogController
with name as method argument. The code would look like this.
namespace sampleSite\controller
{
use hathoora\controller\controller;
/**
* Blog controller
*/
class blogController extends controller
{
/**
* home action of blog
*/
public function orderBy($type = 'name')
{
//
}
}
}
In above examples routing was based on URI, the part after the domain.
However Hathoora PHP Frameworks allows you to change the domain identifier (using regex patterns) which would change the meaning of URI.
To iterate over this point consider the following application specific configuration.
# File HATHOORA_ROOTPATH/boot/config/app.yml
app:
admin:
pattern: '^www.example.com/panel/admin(|/)'
site:
pattern: '^www.example.com(|/)'
default: true # will be used as default
Now consider the following URLs to understand the concept and the controller to which they would be routed to:
http://www.example.com/blog/list
will be handled by app/site/controller/blogController::list
http://www.example.com/panel/admin/list
will be handled by app/admin/controller/listController::index
If default routing is not what you are looking for, then you have two more options for complex routing.
Custom dispatcher is a PHP class defined per application configuration. To enable it, you need to define dispatcher
parameter in configuration as shown below.
# File HATHOORA_ROOTPATH/boot/config/app.yml
app:
admin:
pattern: '^www.example.com/panel/admin(|/)'
# custom routing to be handled by this class
dispatcher:
class: appDispatcher
method: dispatch
site:
pattern: '^www.example.com(|/)'
default: true # will be used as default
In this example requests for site
would be handled normally, however requests for admin would be sent to app/admin/appDispatcher::dispatch
which must return an array containing:
controller
e.g. listControlleraction
e.g. viewarray of params
e.g. ['title', 'id']To see advanced routing in action, consider the following sample code:
# File app/admin/appDispatcher
namespace admin
{
/**
* Custom request dispatcher
*/
class appDispatcher
{
/**
* Custom dispatcher for route request
*
* This function returns array containing:
* - controller class name
* - action name
* - array of params
*/
public function dispatch(\hathoora\container $container)
{
$arrDispatch = null;
$request = $container->getRequest();
$routeRequest = $container->getRouteRequest();
$uri = $request->serverParam('REQUEST_URI');
// URL: /panel/admin/list/12
if (preg_match('~admin/list/(\d+)~i', $uri, $arrMatches)
{
$arrDispatch = array(
'controller' => 'listController',
'action' => 'view',
'params' => array(array_pop($arrMatches));
}
else
{
// following is the same as default routing
$arrUriParts = explode('/', $uri);
array_shift($arrUriParts);
$firstPart = array_shift($arrUriParts);
$arrDispatch = array(
'controller' => 'questionsController',
'action' => $firstPart,
'params' => $arrUriParts);
}
return $arrDispatch;
}
}
}
You can also use Apache rewrite for routing URLs. You would need to set the following params in Apache:
REDIRECT_HTRO
REDIRECT_HTRO_CONTROLLER
REDIRECT_HTRO_ACTION
REDIRECT_HTRO_PARAMS
The following example routes a URL http://mysite.com/posts/1-hello-world
to postsController::view($id, $slug)
.
# File: docroot/.htaccess
RewriteEngine On
# Fancy URL for viewing a post on domains
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteCond %{REQUEST_URI} ((.+?)|)/posts/(\d+)(-\/?(.+?))$ [NC]
RewriteRule .* index.php [E=HTRO:1,E=HTRO_CONTROLLER:postsController,E=HTRO_ACTION:view,E=HTRO_PARAMS[0]:%3,E=HTRO_PARAMS[1]:%5,E=HTRO_PARAMS[2]:%2,L,QSA]
At this moment, it is not possible to set appname using this method.