Improved routing and middleware support for CodeIgniter Framework. Luthier is a set of classes that extends the framework and helps with the development of large and complex applications.
- Laravel-inspired static Route class
- Route groups
- Resource (RESTFul) controllers
- Middleware support
composer require luthier/luthier dev-master
Go to your config/config.php
file and make sure that:
$config['enable_hooks'] = TRUE;
and
$config['composer_autoload'] = TRUE;
Go to your config/hooks.php
file and add this at the top of the script:
use Luthier\Core\Loader as LuthierLoader;
$hook = LuthierLoader::init();
Now you can start adding routes in your config/routes.php
file using the Route
static methods. In the first line add the following:
use Luthier\Core\Route;
And then, for example:
Route::get('foo', ['uses' => 'controller@method', 'as' => 'my-awesome-named-route']);
Once you added all your routes, call the Route::register()
method in the $route
variable to compile all your routes.
Example:
use Luthier\Core\Route;
Route::get('foo', ['uses' => 'controller@method', 'as' => 'my-awesome-named-route']);
Route::home('home@index'); // Default controller
$route = Route::register();
// This compile all the Luthier routes in the CodeIgniter's native format
and you're done!
Route::get('foo/bar', ['uses' => 'testcontroller@index']);
This will be triggered with a GET request over the path foo/bar and will call the index()
method of testcontroller
Route::post('foo/bar', ['uses' => 'testcontroller@another']);
This will be triggered with a POST request over the path foo/bar and will call the another()
method of testcontroller
As you probably noticed, the anterior routes have the same path. Don't worry, Luthier can handle the correct controller over multiples paths depending of the request method.
Luthier works with the most common HTTP Verbs:
- Route::get();
- Route::post();
- Route::put();
- Route::patch();
- Route::delete();
However, you can use any what you want, with the Route::add()
method:
Route::add('OPTIONS', ['uses' => 'foo@bar']);
You can assign names to your routes so you don't have to worry about future path changes:
Route::get('hello/world', ['uses' => 'testcontroller@index', 'as' => 'foo']);
In your views, you can use the function Route::getRouteByName()
with the desired name to retrieve the actual path:
<a href="<?= Route::getRouteByName('foo');?>">My link!</a>
//Produces: <a href="http://myapp.com/hello/world">Link</a>
If you have subdirectories in your controllers folder, you can specify a pseudo-namespace in your routes to indicate to CodeIgniter the actual folder structure to reach the desired controller.
Example:
Route::get('hello/world', ['uses' => 'testcontroller@index', 'as' => 'foo', 'namespace' => 'admin']);
Will be point to application/controllers/admin/Testcontroller.php
You can set a prefix to your routes with the following syntax:
Route::get('hello/world', ['uses' => 'testcontroller@index', 'as' => 'foo', 'prefix' => 'admin']);
So the route will be accesed with the 'admin/hello/world' instead 'hello/world'
You can set the default controller using the method home()
:
Route::home('home@index');
It will set the Home.php
file inside the controllers
folder with the method index()
as your default controller.
You can group your routes in a convenient way using the group()
method:
Route::group(['prefix' => 'foo'], function(){
Route::get('bar', ['uses' => 'test@bar']);
Route::get('baz', ['uses' => 'test@baz']);
});
The route groups allows to encapsulate a set of routes with a prefix and namespace.
While you can omit the namespace, the route group prefix is mandatory.
The middleware allows you to add layers in your requests before accessing the controllers.
All your middleware must be saved in the application/middleware folder. Both the file name and the class name must have the _middleware
suffix .
Basic example:
<?php
// application/middelware/Auth_middleware.php
use Luthier\Core\Middleware;
class Auth_middleware extends Middleware
{
public function __construct()
{
// You MUST call the parent constructor:
parent::__construct();
}
// This is the middleware entry point:
public function run()
{
// The current CodeIgniter instance is available as $this->CI
if( is_null( $this->CI->session->userdata('logged_in') )
{
echo 'You must be logged in to view this resource!';
die;
}
}
}
All your middleware must extend the Middleware
class. The method run()
is the entry point of the middleware.
Example 1: single route middleware
Route::get('foo', ['uses' => 'mycontroller@method', 'middleware' => ['Auth']]);
Example 2: group middleware
Route::group(['prefix' => 'foo', 'middleware' => ['Auth'] ], function(){
Route::get('bar', ['uses' => 'test@bar']);
Route::get('baz', ['uses' => 'test@baz']);
});