Configuring routes using arrays is a common practice in Zend Framework documentation examples. However when the number of routes is increasing I get often lost in arrays and whole route config part starts to be confusing to me.
For readability reasons I'm configuring routes in ini file.
Two ini files
It just requires 2 more ini files located in the same config directory where module.config.php file is located.
The first file router.ini is including second ini file routes.ini. So I don't have to begin every line in routes.ini with router.routes
.
Content of file router.ini
router.router_class = "Zend\Mvc\Router\Http\TranslatorAwareTreeRouteStack"
router.routes.@include = "routes.ini"
Content of file routes.ini (route examples)
jobdetail.type = segment
jobdetail.options.route = "/[:lang/]{job-detail}/:title"
jobdetail.options.constraints.title = "[a-z0-9-]+"
jobdetail.options.defaults.controller = "Jobs\Controller\Jobs"
jobdetail.options.defaults.action = detail
articlestags.type = "Zend\Mvc\Router\Http\Segment"
articlestags.options.route = "/[:lang/]{articles}/t/:tag[/:page]"
articlestags.options.constraints.category = "[a-z0-9-]+"
articlestags.options.constraints.page = "[0-9]+"
articlestags.options.defaults.controller = "Articles\Controller\Articles"
articlestags.options.defaults.action = tags
articlestags.options.defaults.page = 1
articlestags.options.defaults.locale = en
If a value in the ini file contains any non-alphanumeric characters it needs to be enclosed in double-quotes ("). See parse_ini_file() manual.
In Module.php are config files merged together.
namespace Application;
use Zend\Config;
class Module
{
public function getConfig()
{
$config = new Config\Config(include __DIR__ . '/config/module.config.php');
$router = Config\Factory::fromFile(__DIR__ . '/config/router.ini', true);
$config->merge($router);
return $config;
}
......
}