1. Go to this page and download the library: Download bcen/ci-dispatcher library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bcen / ci-dispatcher example snippets
class Welcome extends CI_Controller
{
public function index()
{
$this->load->view('welcome_message');
}
}
class Index extends \Dispatcher\DispatchableController
{
protected $views = 'welcome_message';
}
// config/dependencies.php
$config['container']['userDao'] = function($container) {
return new UserDao();
};
$config['container']['dsn'] = 'mydsn_string';
// sharedContainer will return the same instance throughout the request/response cycle
$config['sharedContainer']['pdo'] = function($container) {
return new PDO($container['dsn']);
};
// user_status.php
class User_Status extends \Dispatcher\DispatchableController
{
// CI-Dispatcher will inject $userDao from config/dependencies.php
// for you.
public function __construct($userDao)
{
$userDao->findUserById(1);
}
}
class DebugFilter
{
public function processRequest(Dispatcher\Http\HttpRequestInterface $req)
{
// do something
}
public function processResponse(Dispatcher\Http\HttpResponseInterface $res)
{
// do something
}
}
// Controller
class User_Status extends \Dispatcher\DispatchableController implements \Dispatcher\Common\CodeIgniterAware
{
public function __construct($userDao)
{
$userDao->findUserById(1);
}
public function setCI($ci)
{
$this->CI = $ci;
}
}
// Middleware
class DebugFilter implements \Dispatcher\Common\CodeIgniterAware
{
public function processRequest(Dispatcher\Http\HttpRequestInterface $req)
{
$cipher = $this->CI->encrypt->encode('plaintext', 'key');
}
public function processResponse(Dispatcher\Http\HttpResponseInterface $res)
{
// do something
}
public function setCI($ci)
{
$this->CI = $ci;
}
}
class Posts extends \Dispatcher\DispatchableController
{
protected $views = array('header', 'post_body', 'footer');
// request handler for GET
public function get($request, $slug)
{
$post = $posts->find($slug);
return $this->renderView(array('post' => $post);
}
// request handler for POST
// providing a default value means that that URI segment is optional.
// e.g. POST http://domain.com/blog/posts/some-slug
// or POST http://domain.com/blog/posts
public function post($request, $slug = null)
{
// do something and return response
}
}