Silex Routing to Controllers -
building app silex using mvc pattern. i'm having bit of issue in regards should doing after have route collection.
here bootstrap app:
<?php namespace app; use igorw\silex\configserviceprovider; use silex\application silex; use symfony\component\routing\route; use symfony\component\routing\routecollection; class bootstrap extends silex { public function __construct() { $this['debug'] = true; $this->registerdefaultparameters(); $this->registerdefaultservices(); $this->registerroutes(); } protected function registerdefaultparameters() { $paths = isset($this['base_path']) ? $this['base_path'] : array(); if (!isset($paths['base'])) { $paths['base'] = realpath(__dir__ . '/../'); } $defaults = array( 'config' => $paths['base'] . '/app/config', 'twig.path' => $paths['base'] . '/public/themes/base/templates' ); foreach ($defaults $key => $value) { if (!isset($paths[$key])) { $paths[$key] = $value; } } $this['paths'] = $paths; } protected function registerdefaultservices() { $this->register( new configserviceprovider($this['paths']['config'] . "/services.yml") ); foreach($this['services'] $servicename => $servicedata) { $this->register( new $servicedata['class'],(array_key_exists('parameters',$servicedata)) ? $servicedata['parameters'] : array() ); } } protected function registerroutes() { $this->register( new configserviceprovider($this['paths']['config'] . "/routes.yml") ); $collection = new routecollection(); foreach($this['routes'] $key => $value) { $collection->add( $key, new route( $value['path'], $value['defaults'], array(), array(), null, null, $value['methods'] )); } $this['routes'] = $collection; } }
so can see set default params. load in services yaml file works fine. register routes again yaml file. loop on each route in yaml file , add route routecollection , re-store in apps ['routes'] entity.
so end $this['routes'] being instance of 'routecollection' routes 'route' instances within it.
now want set controllers routes. issue want set product modularized possible. instead of having controllers folder , models folder containing controllers , models want have core folder core modules in e.g. base module, page module , contact module. each module has own controllers models views etc. similar how magento core is.
so question is:
now have routes set up, steps need take in order make routes instantiate correct controller , action?
i need dynamic rather not declared each individual controller.
any appreciated.
i changed routing function this:
protected function registerroutes() { $this->register( new configserviceprovider($this['paths']['config'] . "/routes.yml") ); $routes = $this['config.routes']; // see first key in yaml file name foreach ($routes $name => $route) { $this->match($route['pattern'], $route['defaults']['_controller'])->bind($name)->method(isset($route['method'])?$route['method']:'get'); } }
everytime app runs tries match 1 fo loaded routes , automatically passes controller!
Comments
Post a Comment