Download the PHP package bcen/ci-dispatcher without Composer
On this page you can find all versions of the php package bcen/ci-dispatcher. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bcen/ci-dispatcher
More information about bcen/ci-dispatcher
Files in bcen/ci-dispatcher
Package ci-dispatcher
Short Description A CodeIgniter plugin for more restful controller
License MIT
Homepage https://github.com/bcen/ci-dispatcher
Informations about the package ci-dispatcher
CodeIgniter-Dispatcher (WIP)
Introduction
CodeIgniter-Dispatcher uses CodeIgniter's _remap function to do an extra routing to a class-based controller instead of function-based.
Example
CodeIgniter's function-based controller
With CodeIgniter-Dispatcher's class-based controller
Installtion
-
Download the
composer.phar
executable or use the installer -
Add this to composer.json
-
Run composer
php composer.phar install
-
Include
autoload.php
in your project and add this toroutes.php
Dispatcher\BootstrapInstaller::run($route)
will create 3 files inside of CodeIgniter'sAPPPATH
, if you want to skip the file creation, addtrue
to to skip the file check.
Features
Poor man's dependency injection
Middlewares, DispatchableController and DispatchableResource will be injected by default.
Middlewares
processRequest
and processResponse
are optional, middleware can implement either one or both
to alter the request/response cycle.
CodeIgniter Awareness
Any class that is created by the Dispatcher can implement CodeIgniterAware
to have CI
injection.
E.g.
Configurations
There are two configuration files, config/dispatcher.php
and config/dependencies.php
.
dispatcher.php
debug
:
Whether to show or hide debug information.
Set true
to show exception, false
to return error 404 response when stuff gone wrong.
middlewares
:
An array of middleware class(es) to be processed before/after dispatch.
When specifying the middlewares, it can be a fully qualified class name if it is autoloaded, otherwise
the class must live under application/middlewares/
in order for CI-Dispatcher to load it (Note: naming convention applies).
dependencies.php
This configuration file is used for DIContainer
to load dependencies and inject them
into Middlewares, DispatchableController and DispatchableResource's constructor.
Note: DIContainer
is a copy cat of Pimple
.
Note:
container
can have anonymous function or simple value like string, array, etc...
sharedContainer
must contian only anonymous function.
Conventions
URL to Controller mappings
URL mapping convention follows almost excatly like CodeIgniter's default strategy.
E.g.
http://domain.com/
maps to application/controllers/index.php
with the class name Index
http://domain.com/about_me
maps to application/controllers/about_me.php
with the class name About_Me
Directory nesting:
http://domain.com/blog
and http://domain.com/blog/ajax/fetch_all_posts
can be mapped to:
Mapping strategy:
CI-Dispatcher will search through each URI segments for the exact file name under application/controllers
.
If it doesn't exists, it will search index.php
under that URI segments.
E.g.
http://domain.com/blog
has the URI segment: blog
.
First CI-Dispatcher will search for application/controllers/blog.php
.
If it doesn't exists, then it will try for application/controllers/blog/index.php
.
URI variable:
Sometime URI segments are not fixed, thus we cannot mapped to a directory or class. However, we can mapp it to the function arguments of the request handler.
E.g.
http://domain.com/blog/posts/this-is-a-crayz-blog-post-about-my-blog/
can be mapped to
application/controllers/blog/posts.php
with the follow class:
Note: The request handler must accept at least one argument for the Request
object.
Middlewares
If middlewares are not autoloaded by class loader like composer
, they must live under
application/middlewares
.
E.g.
debug_filter
will be mapped to application/middlewares/debug_filter.php
with the class name Debug_Filter
.
auth_filter
will be mapped to application/middlewares/auth_filter.php
with the class name Auth_Filter
.
Controller Explained
Any class lives under application/controllers/
and extends Dispatcher\DispatchableController
will be created
and injected by the CI-Dispatcher
.
E.g.
Assumes we are sending a GET
request to http://domain.com/blog/posts/special-post
.
CI-Dispatcher
will map the URI toapplication/blog/posts.php
- Creates a new instance of
Posts
- Invokes the method
get
with arguments:Dispatcher\Http\HttpRequestInterface
and'special-post'
- After getting the response object from
get
, thenCI-Dispatcher
will run through all the middlewares to process the response - Finally, send it to browser by invoking
send
on theresponse
object
At minimal
By default, a GET
request handler is provided, when get
got invoked, it will load the views and data.
So when extending the DispatchableController
, you only need to provide where the view files are by
declaring $views = 'post_view'
or as an array $views = array('header', 'post_view', 'footer')
Overrides getContextData
to send data to the view layer:
Overrides/implements get
, post
, delete
, put
and etc to take complete
control of the request/response handling.
Tests
License
CI-Dispatcher is released under the MIT public license.