Skip to content

Instantly share code, notes, and snippets.

@vluzrmos
Last active August 29, 2015 14:25
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 vluzrmos/218097c8efc2bce011de to your computer and use it in GitHub Desktop.
Save vluzrmos/218097c8efc2bce011de to your computer and use it in GitHub Desktop.
Lumen get intended route.
<?php
// namespace Your\Awesome\Namespace\Here;
use Illuminate\Http\Request;
/**
* Trait HasIntendedRoute.
*/
trait HasIntendedRoute
{
/**
* Get the intended route from request.
*
* @param Request $request
*
* @return array
*/
public function getIntendedRoute(Request $request)
{
$key = $request->method()."/".ltrim($request->path(), "/");
$routes = app()->getRoutes(); // This could be some kind of dependence injection
$route = isset($routes[$key]) ? $routes[$key] : null;
return $route;
}
/**
* Get intended route name.
*
* @param Request $request
*
* @return string
*/
public function getIntendedRouteName(Request $request)
{
$route = $this->getIntendedRoute($request);
return is_array($route) ? $this->getRouteName($route) : '';
}
/**
* Get route name.
*
* @param array $route
*
* @return string
*/
public function getRouteName(array $route)
{
return isset($route['action']['as']) ? $route['action']['as'] : '';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment