简体   繁体   English

Phalcon:得到url params

[英]Phalcon: get url params

I am trying to handle GET params directly in controller with dispatcher, but result is NULL . 我试图直接在带调度程序的控制器中处理GET参数,但结果为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 我的网址就像http://example.com/post/show/2015

I tried as well: 我也试过了:
http://example.com/post/show?year=2015 http://example.com/post/show?year=2015
http://example.com/post/show/year/2015 http://example.com/post/show/year/2015

How should i do that? 我该怎么做?

Dispatcher handles route parameters. Dispatcher处理路由参数。

For this $this->dispatcher->getParam("year"); 对于这个$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. 如果您的网址如下所示: http//example.com/post/show? year = 2015访问您必须使用Request类的年份。

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

'year' - name of the query parameter; 'year' - 查询参数的名称;

'int' - sanitization; 'int' - 消毒;

2012 - default value (if you need it). 2012年 - 默认值(如果您需要)。

Your URL is following the expected /[controller]/[action]/[parameter] format, so all you need to do is make $year a parameter passed to showAction() : 您的URL遵循预期的/[controller]/[action]/[parameter]格式,因此您需要做的就是将$year传递给showAction()

<?php
use Phalcon\Mvc\Controller;

class PostController extends Controller{

   public function showAction($year){

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM