php - Phalcon url parameter missing but action still gets executed -


i using latest phalcon version , finding hard find info on how make action not execute when required parameters missing shown below

class testcontroller extends controllerbase{  public function detailsaction($id) {         $this->view->setvar('theid',$id);     } } 

when access below

http://localhost/phalcon/test/details 

the view still gets executed id missing , when access via

http://localhost/phalcon/test/details/5 

i value 5 .

can please tell me how make "notfound" or when required parameters missing. , have lot of functions such requirements if there general setting in routing or i'd appreciate if me.

thanks guys

you intercept dispatcher event beforeexecuteroute , use introspection of target method:

        if ($event->gettype() == 'beforeexecuteroute') {             $cn = $dispatcher->getnamespacename().'\\'.$dispatcher->gethandlerclass();             $r = new reflectionmethod($cn, $dispatcher->getactivemethod());             $method_params = $r->getparameters();             $url_pars = $dispatcher->getparams();             $pass = true;             $i = 0;             foreach ($method_params $param) {                 if(!$param->isoptional() && !isset($url_pars[$i++])) {                     $pass = false;                 }             }             if(!$pass) {                 return $dispatcher->forward(array('controller' => 'error', 'action' => 'route404'));             }             return true;         } 

this piece of code dispatcher service.

when not provide parameter each action parameters not have default value (isoptional false in case) $pass variable set false , you'll routed 404.

this case, of course, more complex condition allow more fine grained control.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -