PHP code example of bcen / ci-dispatcher

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';
}

    \Dispatcher\Common\BootstrapInstaller::run($route);
    

    \Dispatcher\Common\BootstrapInstaller::run($route, true);
    

// 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;
    }
}



$config['middlewares'] = array(
    'MyProject\\Namespace\\Middlewares\\SomeFilter',
    'debug_filter'
);

$config['debug'] = true;



$config['container'] = array();
$config['sharedContainer'] = array();


$config['container']['dsnString'] = 'dsn:[email protected]';
$config['container']['userDao'] = function($c) {
    return new UserDao($c['dsnString']);
};



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
    }
}

$config['middlewares'] => array('debug_filter', 'auth_filter');



class Posts extends \Dispatcher\DispatchableController
{
    protected $views = 'post_view';
}



class Posts extends \Dispatcher\DispatchableController
{
    protected $views = 'post_view';

    public function getContextData($request)
    {
        $posts = array();
        return array('posts' => $posts);
    }
}



class Posts extends \Dispatcher\DispatchableController
{
    protected $views = 'post_view';

    public function get($request)
    {
        // returns a ViewTemplateResponse
        return $this->renderView(array('post' => $posts));
    }
}

controllers
|
+-- blog
    |-- index.php
    +-- ajax
        |
        +-- fetch_all_posts.php