1

Just started development in the Phalcon PHP framework and am also pretty new to PHP in general. My question is on how to create a request with a route, which I believe I have done, and pass the parameters of the route to the controller action that is linked to the said route. Below I have included the three files that I have been working on and summarize what each one is supposed to do. I also have the end result and where my problem lies directly.

The first file is the index.php file that takes in all route requests for my site.

<?php

//Include all routes on site
foreach (glob("../app/routes/*.php") as $filename)
{
    include $filename;
}

foreach (glob("../app/controllers/*.php") as $filename)
{
    include $filename;
}

//Create routes and initialize routes
$router = new \Phalcon\Mvc\Router();

$router->mount(new PublicRoutes());
$router->mount(new ApiRoutes());
$router->mount(new AdminRoutes());

$router->handle();

$controller = $router->getControllerName();
$action = $router->getActionName();
$params = $router->getParams();

$di = new \Phalcon\DI\FactoryDefault();

$d = new Phalcon\Mvc\Dispatcher();
$d->setDI($di);
$d->setControllerName($router->getControllerName());
$d->setActionName($router->getActionName());
$d->setParams($router->getParams());

$controller = $d->dispatch();

The second file is the actual routes mounted in for my API call which I am testing everything out with.

<?php

class ApiRoutes extends Phalcon\Mvc\Router\Group
{
    public function initialize()
    {

        //Basic api route for pixelpusher
        $this->add(
            "/addhawk/api/:action/:model/:params",
            array(
                "controller" => "api",
                "action" => 1,
                "model" => 2,
                "params" => 3,
            )
        );

    }
}

The third, and final file is the controller class for the API with the only action I am testing right now.

<?php

class ApiController extends \Phalcon\Mvc\Controller
{

    public function handlerAction()
    {
        //Pull in parameters
        echo "<h1>API Handler Entered</h1>";

        $model = $this->dispatcher->getParam("model");

        echo $model;

        //Choose correct api based off of api param
        if( $model == "grid" ) {
            echo 'grid';
        }
        else if ( $model == "admin" ) {
            echo 'admin';
        }
        else {
          //No valid api must have been found for request
        }

        //Return result from api call

        return true;
    }

}

So, the url is "localhost/addhawk/api/handler/grate/view" which results in the following output in html courtesy of line 9 in the ApiController.

Output from API call

There is no print out of the $model variable as it should do. There is also no error so I have no idea why it's not printing. According to the documentation and every resource I have read online, all parameters should be available directly from each controller action thanks to the dispatcher and $di class or something similar. So my question is why can I not access the parameters if everything seems to be saying I should be able to?

ChristopherW
  • 993
  • 1
  • 12
  • 25
  • You're very close to the solution. If this `$model = $this->dispatcher->getParam("model"); echo $model;` does not echo anything, find out what is inside `$this-> dispatcher-> getParam` furthermore I think you have made a typo in your url – UnderDog Oct 28 '15 at 22:45
  • Hi! How did you solve it? I'm facing the same issue now and don't find how to fix it! – Anibal Itriago Jun 03 '18 at 02:30

0 Answers0