Last active
August 29, 2015 14:06
-
-
Save jstoone/2fc8d9b9cc90476bf729 to your computer and use it in GitHub Desktop.
Laravel RouteInfo: A route controller and method helper class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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