Nashvile Symfony Routes Presentation

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    1 Favorite

    Nashvile Symfony Routes Presentation - Presentation Transcript

    1. woot. Symfony Routing!
    2. Basic Routing // /apps/myapp/config/routing.yml foo_route: url: /my/custom/path param: { module: default , action: index } bar_route: url: /another/custom/path param: { module: default action: index , bar: true } foo_bar_route: url: /:foo/:bar param: { module: default , action: index } <?php echo link_to( 'Click Here' , ' @foo_bar_route?foo=that&bar=NOW!' , array ( 'target' => '_blank' )); ?> configuration: in your template: < a href = '/that/NOW!' target = &quot;_blank&quot; > Click Here </ a >
    3. Basic Object Routing // /apps/myapp/config/routing.yml foo_route: class: sfDoctrineRoute url: /member/:username param: { module: default , action: index } options: { type: object , model: sfGuardUser } bar_route: class: sfDoctrineRouteCollection options: { model: Page, module: page , prefix_path: page , column: id , with_wildcard_routes: true } foobar: class: sfDoctrineRouteCollection options: model: Foo module: foo prefix_path: foo/:bar_id column: id with_wildcard_routes: true configuration:
    4. sfDoctrineRouteCollection foobar GET /foobar.:sf_formatfoobar_new GET /foobar/new.:sf_format foobar_create POST /foobar.:sf_format foobar_edit GET /foobar/:id/edit.:sf_format foobar_update PUT /foobar/:id.:sf_format foobar_delete DELETE /foobar/:id.:sf_formatfoobar_show GET /foobar/:id.:sf_formatfoobar_object GET /foobar/:id/:action.:sf_formatfoobar_collection POST /foobar/:action/action.:sf_format Try: $ ./ symfony app:routes myapp It’s RESTful!
    5. More Advanced Routing
      • First Step: Overloading your classes
      Setting a Default Route Class # /apps/myapp/config/config_handlers.yml config/routing.yml: class: myRoutingConfigHandler
      • Configure which classes parse your YAML files
      • Register your handler class in your project configuration
      • Config files are parsed before classes are autoloaded
      Question: How do you configure the config_handlers handler?? do this only if you’re very lazy <?php // /config/config.php $configCache = sfProjectConfiguration :: getActive() -> getConfigCache(); $configCache -> registerConfigHandler( 'config/routing.yml' , 'myRoutingConfigHandler' );
    6. <?php /** * Sets default routing class */ class myRoutingConfigHandler extends sfRoutingConfigHandler { protected function parse ( $configFiles ) { // ... $routes [ $name ] = array ( isset ( $params [ 'class' ]) ? $params [ 'class' ] : $this -> getDefaultRouteClass(), array ( $params [ 'url' ] ? $params [ 'url' ] : '/' , isset ( $params [ 'params' ]) ? $params [ 'params' ] : ( isset ( $params [ 'param' ]) ? $params [ 'param' ] : array ()), isset ( $params [ 'requirements' ]) ? $params [ 'requirements' ] : array (), isset ( $params [ 'options' ]) ? $params [ 'options' ] : array (), )); } } return $routes ; } public function getDefaultRouteClass () { $default = sfConfig :: get( 'app_routing_route_class' ); return $default ? $default : 'sfRoute' ; } } Setting a Default Route Class
      • Extend the sfRoutingConfigHandler class to return a configurable default route class
    7. Subdomain routing # apps/*/config/routing.yml homepage_sub1: url: / param: { module: main , action: homepage1 } class: sfRequestHostRoute requirements: sf_host: sub1.example.com homepage_sub2: url: / param: { module: main , action: homepage2 } class: sfRequestHostRoute requirements: sf_host: sub2.example.com
      • Create a custom route Class (we are using sfRequestHostRoute )
      • specify your subdomain in route requirements
      • note : Don’t Forget! Routes of class sfRoute can still match this url without subdomains.
    8. class sfRequestHostRoute extends sfRequestRoute { public function matchesUrl ( $url , $context = array ()) { if ( isset ( $this -> requirements [ 'sf_host' ]) && $this -> requirements [ 'sf_host' ] != $context [ 'host' ]) { return false ; } return parent :: matchesUrl( $url , $context ); } } Subdomain routing
      • Create sfREquestHostRoute class, extend sfRequestRoute
      • Overload matchesUrl method - This method is called on all routes declared in routing.yml until one returns true
      • check ‘host’ requirement. If it matches, continue to match the url. return false otherwise
    9. Subdomain routing (cont) class sfRequestHostRoute extends sfRequestRoute { // ... public function generate ( $params , $context = array (), $absolute = false ) { $url = parent :: generate( $params , $context , $absolute ); if ( isset ( $this -> requirements [ 'sf_host' ]) && $this -> requirements [ 'sf_host' ] != $context [ 'host' ]) { // apply the required host $protocol = $context [ 'is_secure' ] ? 'https' : 'http' ; $url = $protocol . '://' . $this -> requirements [ 'sf_host' ] . $url ; } return $url ; } }
      • Overload generate method (called when using link_to and url_for methods, etc.)
      • If this route has an sf_host requirement set, add this to the generated url, and prepend to the matching host
      • This will ensure links reroute to the new subdomain
    10. <?php class myRequestHostRoute extends sfRoute { protected $_subdomains = array ( 'foo' , 'bar' ), $_root = 'dev.localhost' , $_default = 'www' ; public function matchesUrl ( $url , $context = array ()) { if (! in_array ( $context [ 'sf_host' ], $this->_subdomains )) { return false ; } return parent :: matchesUrl( $url , $context ); } // ... } Dynamic Subdomain routing
      • Turns Passed Parameter “subdomain” into a subdoman (as long as it’s in the array of specified subdomains)
      note: this class is untested
    11. public function generate ( $params , $context = array (), $absolute = false ) { $url = parent :: generate( $params , $context , false ); // if accessing by ip or different domain, bypass subdomain routing if (! strstr ( $context [ 'host' ], $this -> _root )) { return $url ; } // apply the required host $protocol = $context [ 'is_secure' ] ? 'https' : 'http' ; // if the subdomain is set, and is in our list of known subdomains if ( isset ( $params [ 'subdomain' ]) && in_array ( $params [ 'subdomain' ], $this -> _subdomains )) { $url = $protocol . '://' . $params [ 'subdomain' ] . '.' . $this -> _root . $url ; } else { // use class default if subdomain does not exist $url = $protocol . '://' . $this -> _default . '.' . $this -> _root . $url ; } return $url ; } } Dynamic Subdomain routing (cont) See http://healthandwellness.vanderbilt.edu to see dynamic subdomain routing in action

    + Brent ShafferBrent Shaffer, 1 month ago

    custom

    392 views, 1 favs, 0 embeds more stats

    A presentation for the Nashville Symfony User's Gro more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 392
      • 392 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 1
    • Downloads 7
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories