XF\Mvc\Controller method buildLink does not pass parameters in route

It's unlikely to be a bug as redirects with route parameters are used virtually everywhere within the core, and if that didn't work properly there would be a lot of visible issues.

Please post your route configuration.
 
Add a trailing slash to the route format. You should then be able to achieve a redirect to that route with a category_id param like this:

PHP:
$this->redirect($this->buildLink('addonid/upload', ['category_id' => 1]));

And you should be able to access it in the corresponding controller action via:
PHP:
$params->category_id

If that doesn't work, ensure the redirect URL contains the category ID (/addonid/1/upload) and that it is routing to the expected action (presumably something like \Your\AddOn\Pub\Controller\AddOnId::actionUpload).
 
Add a trailing slash to the route format. You should then be able to achieve a redirect to that route with a category_id param like this:

PHP:
$this->redirect($this->buildLink('addonid/upload', ['category_id' => 1]));

And you should be able to access it in the corresponding controller action via:
PHP:
$params->category_id

If that doesn't work, ensure the redirect URL contains the category ID (/addonid/1/upload) and that it is routing to the expected action (presumably something like \Your\AddOn\Pub\Controller\AddOnId::actionUpload).
It seems that the code of

PHP:
$this->isPost(){ /* Not executing */}

Hovever, i'm calling the action for upload via ajax template, i'm not sure if this is a bug or i'm doing something unexpected.


Tried with
PHP:
$this->request->isPost(); // still false;
to determine if i'm submitting via post.
 
I'm not clear exactly how the action is being called (or the relevance to redirects), but you can use the network inspector of your browser to see how the request is made. It's worth noting that redirects will be GET requests.
 
I'm not clear exactly how the action is being called (or the relevance to redirects), but you can use the network inspector of your browser to see how the request is made. It's worth noting that redirects will be GET requests.
The template where i'm calling is via ajax request, hovever the form submission is via POST.
And i'm using the same method for get & post action. when if it's post, and the category exists, redirect me to the same /upload/ but this time add trailing slash and put category id which i've been choosen from the form.
 
I'm still not clear exactly what your request/response flow is without more comprehensive code demonstrating the issue.

However, if you're making a POST request and it responds with a redirect response, the subsequent redirect request will always be a GET request. While it's technically possible to capture the input and forward it to the GET request, I wouldn't recommend doing that. I would instead rework the code to either process all submissions from the same route, or reroute the response to a different action without redirecting:

PHP:
// reroutes to actionUploadDifferent on the same controller, adjust as necessary
return $this->rerouteController(__CLASS__, 'uploadDifferent', $params);
 
Any route parameters should be passed in the second argument.
This is a little surprising. My understanding was that the second argument should be the entity. Because I seem to have gotten both versions to work:

PHP:
$this->buildLink('foo/bar', $user, ['is_changed' => true]);
// this would generate something like foo/bar/some_title.235?is_changed=true

$this->buildLink('foo/bar2', null, ['is_changed' => true]);
// this would generate something like foo/bar2?is_changed=true
 
Generally route parameters are built from the second argument, and link parameters (ie. query strings) are built from the third argument. The second argument is usually an entity in practice, but it can also just be a regular associative array (entities implement \ArrayAccess, so they behave as an array for route building purposes).

There are some caveats, but they tend to be uncommon (suffixing route parameter types with _p will pull them from the third argument instead, for example).
 
Let me explain, here is example of code:

PHP:
 public function actionUpload(ParameterBag $params)
    {
        if(!$params->category_id)
        {
            return $this->renderCategories();
        }
      
        if($this->isPost()) // dump($this->isPost()); return nothing when i submit the form.
        {
            $categoryId = $this->filter('category_id','int');
            if(empty($categoryId))
            {
                return $this->message(\XF::phrase('no_category_selected'));
            }
            $category = $this->em()->find('addonid\XF:Category',$categoryId);
            if(!$category || !$category->hasPermission('upload'))
            {
                return $this->message(\XF::phrase('no_permission_to_upload'));
            }else{
                return $this->rerouteController(__CLASS__,'Upload',['category_id' => (int) $categoryId);
            }
        }
        $categoryId = $this->filter('category_id','int');
        $category = $this->em()->find('addonid\XF:Category',$categoryId);
        if(!$category)
        {
            return $this->error(\XF::phrase('category_not_found'));
        }
        if(!$category || !$category->hasPermission('upload'))
        {
            return $this->error(\XF::phrase('you_do_not_have_permission_to_upload'));
        }
        $viewParams = [
            'category' => $category
        ];
        return $this->view('','xf_rd_addonid_upload',$viewParams);
 }

The request was made from POST method, but even with rerouteController method doesn't do anything since $this->isPost() method return nothing. Tried to dump($this->isPost()) and somehow doesn't do anything.
 
It seems that there is not a problem within xenforo standard methods and routes. I resolved it by changing checking parameter after $this->isPost() method.

The issue was, when i submit the form, the method first checks for passed parameters in routes which was causing issue where statement for checking if it's post to abort.
 
Back
Top Bottom