Sunday, March 19, 2017

Alto Router not working for controllers

I am trying pass controller name and method to Alto Router map method but it doesnt work

in index.php i have following code

    <?php

    require_once 'vendor/autoload.php';

    use Route\AltoRouter;
    use App\Controllers\HomeController;

    $router = new AltoRouter();

    $router->setBasePath('demo/');
    $router->map('GET','/', 'HomeController#index');
    $router->map('GET', '/php', function(){

        echo 'It is working';
    });
$match = $router->match();

// call closure or throw 404 status
if( $match && is_callable( $match['target'] ) ) {
    call_user_func_array( $match['target'], $match['params'] ); 
} else {

    echo "<pre>";
    print_r($match);
}

Controller

class HomeController extends Controller{

    public function __construct()
    {
        echo "hello, i am a page.";
    }

    public function index(){


        echo "hello, i am a page.";
    }

if i access http://localhost/demo/php then its working but not controller url but its throwing error

Array
(
    [target] => HomeController#index
    [params] => Array
        (
        )

    [name] => 
)

can any one help me how to fix it ? and also is there way to require_once 'vendor/autoload.php'; only once instead of adding in all pages



via iCoders

Advertisement