Skip to content

Get current controller and method #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
58 changes: 58 additions & 0 deletions tests/Support/HelpersTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,65 @@
<?php

use Mockery as m;
use Symfony\Component\HttpFoundation\Request;

class HelpersTest extends PHPUnit_Framework_TestCase {

public function testCurrentActionOnClosure()
{
$router = new Illuminate\Routing\Router;
$router->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)));
Expand Down