Commit b65380d6 authored by Taylor Otwell's avatar Taylor Otwell

Added automatic controller detection.

Signed-off-by: 's avatarTaylor Otwell <taylorotwell@gmail.com>
parent c514ca1e
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
| |
*/ */
Route::get('/, home', function() Route::get('/', function()
{ {
return View::make('home.index'); return View::make('home.index');
}); });
......
...@@ -8,6 +8,7 @@ use Laravel\Bundle; ...@@ -8,6 +8,7 @@ use Laravel\Bundle;
use Laravel\Request; use Laravel\Request;
use Laravel\Redirect; use Laravel\Redirect;
use Laravel\Response; use Laravel\Response;
use FilesystemIterator as fIterator;
abstract class Controller { abstract class Controller {
...@@ -62,6 +63,57 @@ abstract class Controller { ...@@ -62,6 +63,57 @@ abstract class Controller {
} }
} }
/**
* Detect all of the controllers for a given bundle.
*
* @param string $bundle
* @param string $directory
* @return array
*/
public static function detect($bundle = DEFAULT_BUNDLE, $directory = null)
{
if (is_null($directory))
{
$directory = Bundle::path($bundle).'controllers';
}
// First we'll get the root path to the directory housing all of
// the bundle's controllers. This will be used later to figure
// out the identifiers needed for the found controllers.
$root = Bundle::path($bundle).'controllers'.DS;
$controllers = array();
$items = new fIterator($directory, fIterator::SKIP_DOTS);
foreach ($items as $item)
{
// If the item is a directory, we will recurse back into the function
// to detect all of the nested controllers and we will keep adding
// them into the array of controllers for the bundle.
if ($item->isDir())
{
$nested = static::detect($bundle, $item->getRealPath());
$controllers = array_merge($controllers, $nested);
}
// If the item is a file, we'll assume it is a controller and we
// will build the identifier string for the controller that we
// can pass into the route's controller method.
else
{
$controller = str_replace(array($root, EXT), '', $item->getRealPath());
$controller = str_replace(DS, '.', $controller);
$controllers[] = Bundle::identifier($bundle, $controller);
}
}
return $controllers;
}
/** /**
* Call an action method on a controller. * Call an action method on a controller.
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment