Welcome Guest, Not a member yet? Register   Sign In
Luthier-CI: Improved routing + middleware for CodeIgniter 3
#1

(This post was last modified: 05-13-2018, 07:03 PM by andersonsalas.)

Hi!

I want to share my recently rewritten project: it's called Luthier-CI.

What is Luthier-CI?

Luthier-CI is a CodeIgniter plugin that enables Laravel-like routing and introduces the concept of Middleware in our applications.

Key features
  • Clean and easy installation via hooks
  • Global and per-route middleware
  • Advanced routing: prefixes, namespaces, anonymous functions as routes, route groups, cli routes, named parameters, optional parameters, sticky parameters

With Luthier-CI we can, for example, write this:
 
PHP Code:
Route::group('catalog', function(){
 
   Route::get('cars/{category_id}/{filter_by?}''CarsController@catalog');
 
   Route::match(['get','post'], 'bikes/{category_id}/{filter_by?}''BikesController@catalog');
 
   Route::get('airplanes/{category_id}/{filter_by?}''AirplanesController@catalog');
});

Route::post('this/is/a/{very?}/{large?}/{route?}/{example?}''TestController@foo'); 

Instead this:
 
PHP Code:
$route['catalog/cars/(:any)']['GET'] = 'CarsController/catalog/$1';
$route['catalog/cars/(:any)/(:any)']['GET'] = 'CarsController/catalog/$1/$2';
$route['catalog/bikes/(:any)']['GET'] = 'BikesController/catalog/$1';
$route['catalog/bikes/(:any)']['POST'] = 'BikesController/catalog/$1';
$route['catalog/bikes/(:any)/(:any)']['GET'] = 'BikesController/catalog/$1/$2';
$route['catalog/bikes/(:any)/(:any)']['POST'] = 'BikesController/catalog/$1/$2';
$route['catalog/airplanes/(:any)']['GET'] = 'AirplanesController/catalog/$1/$2';
$route['catalog/airplanes/(:any)/(:any)']['GET'] = 'AirplanesController/catalog/$1/$2';
$route['this/is/a']['GET'] = 'TestController/foo';
$route['this/is/a/(:any)']['POST'] = 'TestController/foo/$1';
$route['this/is/a/(:any)/(:any)']['POST'] = 'TestController/foo/$1/$2';
$route['this/is/a/(:any)/(:any)/(:any)']['POST'] = 'TestController/foo/$1/$2/$3';
$route['this/is/a/(:any)/(:any)/(:any)/(:any)']['POST'] = 'TestController/foo/$1/$2/$3/$4'
 
... or use an anonymous function instead a controller:

PHP Code:
Route::get('test', function(){
 
   echo 'Hello world!';
}); 

... or even perform the same action with all routes using a middleware:

PHP Code:
Route::middleware(function(){
 
   echo 'This will be executed on every route below!';
}, 
'pre_controller');

Route::group('foo', function(){
 
   Route::get('bar', function(){
 
       echo 'Test';
 
   });
 
   Route::get('baz', function(){
 
       echo 'Test 2';
 
   }, ['middleware' => ['Auth']]); # Only this route will run the 'Auth' middleware!
}); 

More information (Documentation, installation instructions, etc.)

View Luthier-CI on GitHub

It is a somewhat complex project so any suggestion, opinion, or even donation is welcome!

Tested in PHP 5/7 and CodeIgniter 3.1.7 / 3.1.8 (apparently everything works  Big Grin )
Reply


Messages In This Thread
Luthier-CI: Improved routing + middleware for CodeIgniter 3 - by andersonsalas - 04-17-2018, 09:52 PM



Theme © iAndrew 2016 - Forum software by © MyBB