From 162552b8790fa3cfa13f94787a19da4dbabbe144 Mon Sep 17 00:00:00 2001 From: Anderson Salas <6117373+andersonsalas@users.noreply.github.com> Date: Tue, 17 Apr 2018 23:35:03 -0400 Subject: [PATCH] Version 0.1.5 --- readme.md | 74 ++++++++++++++++------------- src/Resources/DefaultController.php | 4 +- 2 files changed, 43 insertions(+), 35 deletions(-) diff --git a/readme.md b/readme.md index 7e68ac3..f7b4111 100644 --- a/readme.md +++ b/readme.md @@ -5,24 +5,24 @@ [![Latest Unstable Version](https://poser.pugx.org/luthier/luthier/v/unstable?format=flat-square)](https://packagist.org/packages/luthier/luthier) [![License](https://poser.pugx.org/luthier/luthier/license?format=flat-square)](https://packagist.org/packages/luthier/luthier) -Laravel-like routing and Middleware support for CodeIgniter 3. **Luthier-CI** makes the development of CodeIgniter apps even more enjoyable and simple! - -A design goal of Luthier-CI is to have the less side effects possible and be deeply integrated to the framework, so basically your app must be working just like before the installation, no matters what libraries, hooks, helpers o third party packages you have currently. +Laravel-like routing and Middleware support for CodeIgniter 3. **Luthier-CI** makes the development of CodeIgniter apps even more enjoyable and simple! + +A design goal of Luthier-CI is to have the less side effects possible and be deeply integrated to the framework, so basically your app must be working just like before the installation, no matters what libraries, hooks, helpers o third party packages are enabled. ## Features -* Clean and easy instalation via hooks +* Clean and easy installation via hooks * Global and per-route middleware -* Advanced routing: prefixes, namespaces, anonymous functions as routes, route groups, named parameters, optional parameters, default parameter values and "sticky" parameters +* Advanced routing: prefixes, namespaces, anonymous functions as routes, route groups, cli routes, named parameters, optional parameters, sticky parameters ## Requirements -* PHP 5 and 7 are compatible +* PHP >= 5.6.0 (PHP 7 compatible) * CodeIgniter 3.x ## Installation -(**Note:** this tutorial is assuming that you have a fresh CodeIgniter installation) +**Note:** this tutorial is assuming that you have a fresh CodeIgniter installation #### Get Luthier-CI with Composer @@ -55,7 +55,7 @@ defined('BASEPATH') OR exit('No direct script access allowed'); $hook = Luthier\Hook::getHooks(); ``` -Set the Luthier routes: +Set the Luthier-CI routes: ```php Link ``` -If the route have parameters, pass a second argument to the function with an array of their values: +If the route has parameters, pass a second argument to the function with an array of their values: ```php 'value2', 'param2' => 'value2' ... ]); ?> @@ -142,7 +151,7 @@ If the route have parameters, pass a second argument to the function with an arr What if you want to define a global route parameter and make it available to all inherited routes? This can be done with help of *sticky* parameters. A sticky parameter starts with an underscore `_` and has a few singularities: * Is NOT passed in the arguments of the pointed controller's method -* The parameter value is the same of the current route value and is setted by default if is not present, making it *sticky* (if the subsequent routes shares that parameter, of course) +* The parameter value is the same of the current route value and is set by default if is not present, making it *sticky* (if the subsequent routes shares that parameter, of course) Here's an example: @@ -219,7 +228,7 @@ Route::get('hello/world', 'testcontroller@index', ['prefix' => 'admin']); #### Route Groups Group routes is possible with the `Route::group()` method. All routes -inside the group will share the *prefix* (first argument) and, optionally, another properties (*namespace*, *middleware*, etc.) +inside the group will share the *prefix* (first argument) and, optionally, another property (*namespace*, *middleware*, etc.) ```php // Prefix only @@ -248,17 +257,17 @@ Route::set('default_controller', 'welcome/index'); ## Middleware -Luthier-CI adds the concept of middleware to the framework. Think on the middleware as a set of "layouts" that the user will pass thru with every request until reach to the desired resource. You can, for example, perform a user validation before enter to a controller, and redirect it to another place in case of failure. An advantage of the middleware is that you can assign it on a specific route/group or even define it as global middleware, and will be executed on all routes! - -In fact, the middleware is an extension of the controller, because the framework singleton is built at this point. So you will be able of execute all the libraries/functions that are normally available inside the controllers. +Luthier-CI adds the concept of middleware to the framework. Think on the middleware as a set of "layouts" that the user will pass thru with every request until reach to the desired resource. You can, for example, perform a user validation before entering to a controller, and redirect it to another place in case of failure. An advantage of the middleware is that you can assign it on a specific route/group or even define it as global middleware + +In fact, the middleware is an extension of the controller, because the framework singleton is built at this point. So, you will be able of execute all the libraries/functions that are normally available inside the controllers. #### Middleware execution points In global context, there's two execution points available: -* **pre_controller**: middleware will be executed right after the controller constructor, *BUT* before any controller action were performed. Please notice that the controller constructor is called at this point, because the own CodeIgniter's execution sequence (wich Luthier-CI don't modify) so if you need to do some actions *before* any middleware is executed, you must define a method called *preMiddleware()* in your controller and Luthier-CI will call it first. +* **pre_controller**: middleware will be executed right after the controller constructor, *BUT* before any controller action were performed. Please notice that the controller constructor is called at this point, because the own CodeIgniter's execution sequence (which Luthier-CI don't modify) so if you need to do some actions *before* any middleware is executed, you must define a method called *preMiddleware()* in your controller and Luthier-CI will call it first. * **post_controller**: middleware will be executed exactly in the `post_controller` hook point. - + In a route context, there's only one execution points and works as same as the **pre_controller** point in global context. #### Middleware definition @@ -276,12 +285,12 @@ Route::group('site', ['middleware' => ['Admin']], function(){ # Global middleware: Route::middleware('Admin', 'pre_controller'); -```` +``` The middleware files must be saved in the `application/middleware` folder. If not exists, you must create it first. A middleware file is any php class with a public `run()` method, which is the entry point. It's strongly advised to name all your middleware with CamelCase and avoid name conflicts with your controllers. - + #### Manually running middleware - + Use the `middleware->run()` method in your controllers to run a middleware on-demand: ```php @@ -290,19 +299,19 @@ Use the `middleware->run()` method in your controllers to run a middleware on-de class MyController extends CI_Controller { public function __construct() - { + { parent::__construct(); $this->middleware->run('Admin'); } - + // ... } -```` - +``` + #### Callbacks as middleware - + You can use callbacks in middleware definitions, and works exactly as route callbacks: - + ```php Route::middleware(function(){ ci()->load->library('twig'); @@ -310,9 +319,8 @@ Route::middleware(function(){ ``` ## Roadmap - -* CLI Routes :( -* Write a better and in-depth documentation + +* Write a better and in-depth documentation * Probably more, feel free to make all your suggestions! ## Donate diff --git a/src/Resources/DefaultController.php b/src/Resources/DefaultController.php index 102ed18..f92a4e6 100644 --- a/src/Resources/DefaultController.php +++ b/src/Resources/DefaultController.php @@ -3,8 +3,8 @@ /** * Luthier default controller - DO NOT EDIT OR REMOVE THIS FILE! * - * Because CodeIgniter check the existence of the file/class/method of the controller - * pointed in all routes, this allows to create"dynamic" routes without hacking/extending + * Because CodeIgniter checks the existence of the file/class/method of the controller + * pointed in all our routes, this allows to create dynamic routes without hacking/extending * the CI_Router class. */