1

I'm trying to add some custom routes for my plugin, but it's throwing a 400 CHttpException for invalidActionParams when accessing largeupload/folders/2 as an example.

public function registerCpRoutes ()
{
return array(
    'largeupload'                           => array( 'action' => 'largeUpload/index' ),
    'largeupload/folders'                   => array( 'action' => 'largeUpload/folder/index' ),
    'largeupload/folders/new'               => array( 'action' => 'largeUpload/folder/edit' ),
    'largeupload/folders/(?P<folderId>\d+)' => array( 'action' => 'largeUpload/folder/edit' ),
);
}

My controller is called LargeUpload_Folder, and my action actionEdit().

public function actionEdit ($variables = [ ])
{
    /* Simplified */
    if ( isset($variables['folderId']) ) {
    }
    else {
    }

    $this->renderTemplate('largeupload/LargeUpload_Edit', [
    ]);
}

If I remove $variables = [ ] from the action parameters, it doesn't return an error anymore.

Any ideas?

2

I believe this is caused by not including array in the function parameter, preceding the variable.

Changing to public function actionEdit (array $variables = [ ]) should do the trick.

As a bonus - can anyone explain why? Seems like the $variables variable needs to be cast as an array?

| improve this answer | |
  • Yes indeed. Would love to know why! – Fred Carlsen Jan 28 '17 at 14:29
2

Another option is to retrieve the route variables inside your controller action and not pass anything into the function as a parameter :

public function actionEdit() 
{
    $routeParams = craft()->urlManager->getRouteParams();
    $folderId = $routeParams['variables']['folderId'];

    //Do stuff with $folderId

    $this->renderTemplate('largeupload/LargeUpload_Edit', []);
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.