Routing in Lepton
Introduction
The earlier versions of Lepton, the routing has been somewhat static in that it has mostly been controlled from within the .htaccess file. With the introduction of v0.2 this will be made much more dynamic, with added support for multidomain routing. What that means is that you can now from within a single project host several subdomains with shared cookies and a shared application base, but depending on the domain route it to the controller and method of choice.
Configuration
The routing is controlled by the lepton.mvc.* keys:
config::set('lepton.mvc.router',null);
config::set('lepton.mvc.routedomain',false);
config::set('lepton.mvc.routedomainbase',null);
The lepton.mvc.router key should be set to the classname of your desired router. This could be one of the builtin routers (such as BasicRouter) or a custom router (inheriting the IRouter interface).
If lepton.mvc.routedomain is true the domain name that received the request will be passed to the router. If you only have one site in the application, this is normally not needed. If this option is enabled, you can also use the lepton.mvc.routedomainbase to strip off a fixed part of the domain name:
Setting routedomainbase to foo.tld would for the request domain bar.foo.tld return simply bar. For a request to foo.tld, it would instead return null
Implementing a router
The task of a router
A router is nothing more than a glorified class that makes sure that the request ends up at the right controller and method. It does this by parsing the domain info as well as ssl info and the request URI.
How the default router works
The default builtin router (that is used if the router class is set to null) simply "pops" the two leftmost parameters off the URI:
/foo/bar/baz/xyzzy
Controller -' | '---.-----'
Method--------' |
Arguments---------'
After doing this, it calls on Controller::invoke() with the controller and method to invoke.
Example router
<?php // Derive your router from the Router base class class MyRouter extends Router { public function routeRequest($uri) { // Switch based on the first segment of the URL switch($this->getSegment(0)) { // Requests to /sv/foo or /en/foo should set the language before // invoking 'foo' in the index controller. case 'sv': case 'en': lang::setLanguage($this->getSegment(0)); Controller::invoke('index', $this->getSegment(1), $this->getSegmentSlice(2)); break; // All requests to /admin should go to the admin controller case 'admin': Controller::invoke('admin', $this->getSegment(1), $this->getSegmentSlice(2)); break; // And everything else to the index controller. default: Controller::invoke('index', $this->getSegment(0), $this->getSegmentSlice(1)); break; } } } ?>
Save the router in the application/ folder, and place a 'require' entry into the application.php file within the lepton/ directory.
application.php
<?php require('application/indexrouter.php'); ?>
Methods available to routers
$this->setBase($base)
Changes the base path of the invoked controller. The base path is inserted between controllers/ and the expected controller filename. Setting this to foo and invoking the controller bar would thus lead to a request for controllers/foo/bar.php.
$this->setBase('files');
$this->getSegment($index)
Retrieves the specific segment from the requested query string.
// Having /foo/bar/baz this would return bar $i = $this->getSegment(2);
$this->getSegmentSlice($start,[$length])
Returns the specified set of segments from the requested query string.
// Having /foo/bar/baz this would return an array with 'bar' and 'baz' $s = $this->getSegmentSlice(1);
$this->hasSegment($index)
Returns true if the segment exists.
// Having /foo/bar/baz this would return true: $x = $this->hasSegment(2);
$this->getSegmentCount()
Returns the number of segments.
$this->isSecure()
Returns true if the request is made over SSL/HTTPS.
$this->getDomain()
Returns the domain of the request.
$this->getURI()
Returns the URI in whole.
