Skip to content

Instantly share code, notes, and snippets.

@aubricus
Last active December 20, 2015 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aubricus/6109083 to your computer and use it in GitHub Desktop.
Save aubricus/6109083 to your computer and use it in GitHub Desktop.
Zend routes vary in appearance

Zend Route Object Insanity

Zend route objects seem to vary in appearance given certain conditions.

Notes:

  • Route object obtained from ControllerInstance->params()->fromRoute()
  • Other Zend objects returning either a route or an array with a route seem to suffer the same problem.
  • This could be a route configuration problem, but logging here for reference.
  • Below I've cataloged what I've found so far.

Zend Route for Index of a Sub-Section (No Ending Slash)

Returned by: http://{root}.com/{url-segment}/{url-segment}

Example: http://devs.com/recent/aubricus

Note: The absence of an ending slash.

array(3) {
  ["controller"]=>
  string(21) "Dev\Controller\Index"
  ["action"]=>
  string(5) "index"
  ["devName"]=>
  string(5) "aubricus"
}

Zend Route for Index of a Sub-Section (Ending Slash)

Returned by: http://{root}.com/{url-segment}/{url-segment}/

Example: http://devs.com/recent/aubricus/

Note: The ending slash.

array(5) {
  ["__NAMESPACE__"]=>
  string(15) "Dev\Controller"
  ["controller"]=>
  string(21) "Dev\Controller\Index"
  ["action"]=>
  string(5) "index"
  ["devName"]=>
  string(5) "aubricus"
  ["__CONTROLLER__"]=>
  string(5) "Index"
}

Zend Route for Sub-Section of a Sub-Section (No Ending Slash)

Returned by: http://{root}.com/{url-segment}/{url-segment}/{url-segment}

Example: http://devs.com/recent/aubricus/about

Note: The absence of an ending slash.

Note: This same route with an ending slash returns a 404, where the index route with an ending slash does not.

array(5) {
  ["__NAMESPACE__"]=>
  string(15) "Dev\Controller"
  ["controller"]=>
  string(21) "Dev\Controller\About"
  ["action"]=>
  string(5) "index"
  ["devName"]=>
  string(5) "aubricus"
  ["__CONTROLLER__"]=>
  string(5) "about"
}
<?php
namespace Application\Util;
/**
* ZendRoute
*
* Simple static utils for working with Zend Routes
*/
class ZendRouteUtils
{
/**
* Zend route objects can vary in appearance, this method attempts to
* unify discrepencies by applying defaults to missing properties.
*
* @param array $route The original route.
* @return array The modified route.
*
* see: https://gist.github.com/aubricus/6109083
*/
public static function sanitize_route($route)
{
$controller = $route['controller'];
$controllerSegments = explode("\\", $controller);
$controllerSegLen = count($controllerSegments);
// var_dump($controllerSegments);
// var_dump($controllerSegLen);
if(!in_array("__CONTROLLER__", $route)) {
// Sanitize missing __CONTROLLER__ value
// Derive value from last segment of $controller
$value = ucfirst($controllerSegments[$controllerSegLen -1]);
$route["__CONTROLLER__"] = $value;
}
if(!in_array("__NAMESPACE__", $route)) {
// Sanitize missing __NAMESPACE__ value
// Derive value from all but last segment from $controller
$value = array_slice($controllerSegments, 0, $controllerSegLen -1);
$value = implode("\\", $value);
$route["__NAMESPACE__"] = $value;
}
// Return sanitized route
return $route;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment