diff --git a/src/Illuminate/Support/helpers.php b/src/Illuminate/Support/helpers.php index ddffdf7f9c50..ea20226557e1 100644 --- a/src/Illuminate/Support/helpers.php +++ b/src/Illuminate/Support/helpers.php @@ -45,6 +45,18 @@ function app_path() return app('path'); } +/** + * Get the current controller and method. + * + * @return mixed + */ +function current_action() +{ + $currentRoute = app('router')->getCurrentRoute(); + + return ($currentRoute === null) ? null : $currentRoute->getOption('_uses'); +} + /** * Divide an array into two arrays. One with keys and the other with values. * diff --git a/tests/Support/HelpersTest.php b/tests/Support/HelpersTest.php index 5b570f1619d6..53ba46f6e8cc 100644 --- a/tests/Support/HelpersTest.php +++ b/tests/Support/HelpersTest.php @@ -1,7 +1,65 @@ get('/', function() {}); + + $request = Request::create('/', 'GET'); + + $router->dispatch($request); + + $app = m::mock('Illuminate\Foundation\Application'); + $app->shouldReceive('make')->once()->with('router')->andReturn($router); + + Illuminate\Support\Facades\Facade::setFacadeApplication($app); + + $this->assertNull(current_action()); + } + + public function testCurrentActionOnController() + { + $router = new Illuminate\Routing\Router; + + $container = m::mock('Illuminate\Container\Container'); + + $controller = m::mock('stdClass'); + $controller->shouldReceive('callAction')->once()->with($container, $router, 'getIndex', array()); + + $container->shouldReceive('make')->once()->with('Controllers\Home')->andReturn($controller); + + $router->setContainer($container); + $router->get('/', 'Controllers\Home@getIndex'); + + $request = Request::create('/', 'GET'); + + $router->dispatch($request); + + $app = m::mock('Illuminate\Foundation\Application'); + $app->shouldReceive('make')->once()->with('router')->andReturn($router); + + Illuminate\Support\Facades\Facade::setFacadeApplication($app); + + $this->assertEquals('Controllers\Home@getIndex', current_action()); + } + + public function testCurrentActionOnNoCurrentRoute() + { + $router = new Illuminate\Routing\Router; + + $app = m::mock('Illuminate\Foundation\Application'); + $app->shouldReceive('make')->once()->with('router')->andReturn($router); + + Illuminate\Support\Facades\Facade::setFacadeApplication($app); + + $this->assertNull(current_action()); + } + public function testArrayDot() { $array = array_dot(array('name' => 'taylor', 'languages' => array('php' => true)));