Skip to content

Instantly share code, notes, and snippets.

@jstoone
Last active August 29, 2015 14:06
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 jstoone/2fc8d9b9cc90476bf729 to your computer and use it in GitHub Desktop.
Save jstoone/2fc8d9b9cc90476bf729 to your computer and use it in GitHub Desktop.
Laravel RouteInfo: A route controller and method helper class
<?php namespace JakobSteinn\Core;
use Illuminate\Routing\Router;
use Illuminate\Support\Str;
class RouteInfo {
/**
* @var Router
*/
private $router;
/**
* @var Str
*/
private $stringHelper;
/**
* @param Router $router
*/
function __construct (Router $router, Str $stringHelper)
{
$this->router = $router;
$this->stringHelper = $stringHelper;
}
/**
* Method to get the current controller in use
*
* @return string
*/
public function controller ()
{
return $this->getControllerName();
}
/**
* Method to get the current controller method in use
*
* @return string
*/
public function action ()
{
return $this->getActionName();
}
/**
* Get controller and action values as an array
*
* @return array
*/
public function toArray ()
{
$values = [
'action' => $this->getRouteActionSplit(),
'controller' => $this->getRouteActionSplit(true)
];
return $values;
}
/**
* Get the controller name
*
* @return string
*/
private function getControllerName ()
{
return $this->getRouteActionSplit(true);
}
/**
* Get the controller action name
*
* @return string
*/
private function getActionName ()
{
return $this->getRouteActionSplit();
}
/**
* Decorate and return specified controller or action
*
* @param bool $getController
*
* @return string
*/
private function getRouteActionSplit ($getController = false)
{
list($routeController, $routeAction) = explode('@', $this->router->currentRouteAction());
$piece = $this->stringHelper->camel($routeAction);
if ($getController)
{
$piece = $this->stringHelper->lower(str_replace('Controller', '', $routeController));
}
return $piece;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment