htmlburger / wpemerge

A modern, MVC-powered WordPress as a CMS workflow. 🚀
https://wpemerge.com/
GNU General Public License v2.0
453 stars 38 forks source link

Unknown named parameter $action when using AJAX routes #33

Closed czernika closed 2 years ago

czernika commented 2 years ago

Version

Expected behavior

AJAX routes works as expected - send AJAX request and get response

Actual behavior

Error 500 - Unknown named parameter $action

Steps to reproduce (in case of a bug)

  1. Define ajax route
'ajax' => [
      'definitions' => BROCOOLY_THEME_PATH . 'routes/ajax.php',
      'attributes'  => [
          'namespace' => 'Theme\\Http\\Controllers\\',
      ],
  ],
  1. Create route within ajax.php
App::route()->post()->where( 'ajax', 'action_name', true, true )
    ->handle( function() {
        return App::output( 'Hello World!' ); // nothing fancy
    } );
  1. Send request with axios

This step checked with "usual" wp_ajax_ actions and its works so nothing wrong with it

const formData = new FormData(form);
formData.append( 'action', 'action_name' );

axios.post(ajax.url, formData)
    .then( ({ data }) => {
        console.log(data);
    })
  1. Send request - getting 500 error "Unknown named parameter $action"

Comments

After some debugging I found out that it pushes [ 'action' => 'action_name'] route argument within handle() method of HttpKernel class. As it passes to Handler's execute method which has NOT named arguments (it is uses func_get_args()) I guess this is what causing an issue. As a quick fix I had to change a little HttpKernel::handle method by adding few extra lines

if ( wp_doing_ajax() ) {
    $route_arguments = array_values( $route_arguments ); // ensure no named params passed
}

Now it's works (I can see "Hello World" in my console) but I had to change core file. Maybe there is another fix or am I doing AJAX wrong?

atanas-dev commented 2 years ago

Hi there,

You're doing everything right :)

This is a known PHP 8 issue and there's a fix in master for it (https://github.com/htmlburger/wpemerge/commit/b2c8a1b47ba476fc023def17344146816fb18d67) but a full PHP 8-supporting release is not available quite yet because of some WordPress-related complications and lack of time.