Skip to content

Instantly share code, notes, and snippets.

@cereal-s
Last active August 23, 2016 14:04
Show Gist options
  • Save cereal-s/5d5e8ecca4baa335b49d90bc1f782bc8 to your computer and use it in GitHub Desktop.
Save cereal-s/5d5e8ecca4baa335b49d90bc1f782bc8 to your computer and use it in GitHub Desktop.
_route() function for SlimPHP 3.4.2, it returns the path for the named route
<?php
if( ! function_exists('_route'))
{
/**
* Return the path for the named route
*
* @see http://www.slimframework.com/docs/objects/router.html#route-names
* @param string $name
* @param array $parameters
* @return mixed
*/
function _route($name = '', $parameters = [])
{
global $app;
if($app instanceof Slim\App)
return $app->getContainer()
->get('router')
->pathFor($name, $parameters);
return FALSE;
}
}
@cereal-s
Copy link
Author

cereal-s commented Jun 23, 2016

If in routes.php there is a route named fruit.index, like in this example:

$app->get('/fruit/{name}', '\App\Controllers\FruitController:index')->setName('fruit.index');

And in the view we want to print the path:

echo _route('fruit.index', ['name' => 'fuji_apples']);

The result will be /fruit/fuji_apples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment