We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Annotations Router Parameters give PHP Missing Argument

I am trying to parse an argument from the route like this:

    /**
     * @Get("/lft/{id:[1-9]+}")
     */
        public function getByLftAction( $id )
        {
        }

But I keep geeting:<b>Warning</b>: Missing argument 1.

What am I doing wrong?



8.1k

See your Router service. You may set URI_SOURCE_SERVER_REQUEST_URI in router. Like this : $router->setUriSource(Router::URI_SOURCE_SERVER_REQUEST_URI); Or check your .htaccess file. I use REQUEST_URI is usually.



2.6k

Good point, I am now setting the router source explicitly but I still encounter the same problem.

Any other idea on what I can check?

nginx

    location / {
        try_files $uri $uri/ @rewrite;
    }
    location @rewrite {
        rewrite ^(.*)$ /index.php?_url=$1;
    }

service

    $router = new Router(false);
    $router->setUriSource(Router::URI_SOURCE_GET_URL);

controller

    /**
     * @Get("/lft/{lft}")
     */
        public function getByLftAction( $lft  )
        {
            var_dump($this->request->getQuery());
            echo $lft;
        }

result

<b>Warning</b>:  Missing argument 1 for ..
array(1) {
  ["_url"]=>
  string(13) "/flight/lft/1"
}


8.1k

No, no, no! Nowise any rewrite in nginx config.

location /  {
    try_files $uri  $uri/  /index.php;
  }

 location ~* \.php$ {
   try_files $uri =404;
   fastcgi_pass 127.0.0.1:9000;
   include /etc/nginx/fastcgi_params;
   fastcgi_split_path_info  ^(.+\.php)(/.+)$;
   fastcgi_index index.php;
   fastcgi_param  SCRIPT_FILENAME /home/your_web_public_directory/index.php;
   fastcgi_param  SCRIPT_NAME     /index.php;
   fastcgi_param  QUERY_STRING    $uri$args;
   fastcgi_param  REQUEST_METHOD  $request_method;
   fastcgi_param  CONTENT_TYPE    $content_type;
   fastcgi_param  CONTENT_LENGTH  $content_length;
   fastcgi_param  REQUEST_BODY    $request_body;
 }


2.6k

Ok, I have tried with your config + adding:

  root /home/your_web_public_directory;

However, it didnt solve the problem. I got the first config from the documentation.



8.1k

This is config from real server:

server {
  listen   80; 
  server_name mysite.com;
  autoindex off;
  set $root_path  '/home/john/www/mysite/public';
  root $root_path;
  index index.php;

  location ~ assets/(css|js|img|video)/ {
    access_log off;
    expires 360d;
  }

  location ~ favicon.ico$ {
    access_log off;
    expires 360d;
  }

  location /  {
    try_files $uri  $uri/  /index.php;
  }

 location ~* \.php$ {
   try_files $uri =404;
   fastcgi_pass 127.0.0.1:9000;
   include /etc/nginx/fastcgi_params;
   fastcgi_split_path_info  ^(.+\.php)(/.+)$;
   fastcgi_index index.php;
   fastcgi_param  SCRIPT_FILENAME /home/john/www/mysite/public/index.php;
   fastcgi_param  SCRIPT_NAME     /index.php;
   fastcgi_param  QUERY_STRING    $uri$args;
   fastcgi_param  REQUEST_METHOD  $request_method;
   fastcgi_param  CONTENT_TYPE    $content_type;
   fastcgi_param  CONTENT_LENGTH  $content_length;
   fastcgi_param  REQUEST_BODY    $request_body;
 }

} ## server config end

Thsi config work with

$router->setUriSource(Router::URI_SOURCE_SERVER_REQUEST_URI);


8.1k

P.S. Lighttpd work with Phalcon very nice + video streaming (HTML5).



2.6k

Thanks! I will use this configuration. However it still does not solve the problem :( I verified by running the php built in web server:

php -S 0.0.0.0:8000 -t . .htrouter.php

The route itself works because I have tested by changing the regex with [a-z] and [1-9]. I am beginning to think it is the passing of the parameter to the controller that doesnt work.



2.6k

Now it works! I didnt have a dispatcher service for the module I was routing.

I used your Phalconskeletonmulti to verify that it was possible to pass the parameters at all. It was :) After this I could compare the difference and noticed the dispatcher.

Thanks for your help and contribution!

EDIT: The real problem was the dispatch:beforeDispatchLoop event taken from https://docs.phalcon.io/en/latest/reference/dispatching.html#preparing-parameters This code will set new parameters from the request and must be considered or removed when implementing the routes.



8.1k

I'm glad to help you.