Search code examples
phpphalcondispatcher

Phalcon: get url params


I am trying to handle GET params directly in controller with dispatcher, but result is NULL.

<?php
use Phalcon\Mvc\Controller;

class PostController extends Controller{

   public function showAction(){
       $year = $this->dispatcher->getParam("year");

       var_dump($year); //returns NULL;
   }
}

My url is like http://example.com/post/show/2015

I tried as well:
http://example.com/post/show?year=2015
http://example.com/post/show/year/2015

How should i do that?


Solution

  • Dispatcher handles route parameters.

    For this $this->dispatcher->getParam("year"); to work you need to define "year" in your route:

    $router->add('/post/show/{year}', 'Posts::show')->setName('postShow');
    

    If your url looked like this: http://example.com/post/show?year=2015 to access the year you have to use the Request class.

    $this->request()->getQuery('year', 'int', 2012);
    

    'year' - name of the query parameter;

    'int' - sanitization;

    2012 - default value (if you need it).