Search code examples
phpsymfonyprestashopprestashop-1.7prestashop-modules

Prestashop wrong token generated for symfony created Admin controller


Please if some piece of information is missing or I totally screwed up the explanation of the problem tell me!

Hi, I wanted to create an Admin controller to be used for AJAX requests. I created it with this document on the Prestashop dev docs website. The problem is that I for the love of god cannot create the right token for accessing that controller. I tried the function Tools::getAdminTokenLite("AdminModules") I tried the normal Tools:GetAdminToken and always the token is not correct.

What I tried: http://localhost:8001/admin395pdq48a/modules/STLuploader/images?controller=ImageSTL&action=getimages&token=8346c9bf702c3fc0712271250925e419

token is generated by getAdminTokenLite described above. Return is 404 not found, I would say that is weird why 404 I tried formulating the name of controller in different ways nothing worked?

http://localhost:8001/admin395pdq48a/modules/STLuploader/images?action=getimages&token=8346c9bf702c3fc0712271250925e419

Return is 302 and invalid token.
Only thing that works is when I click on I accept the security risk and URL is: http://localhost:8001/admin395pdq48a/modules/STLuploader/images?_token=g5AZ8GEscofUtruxoKBeTyQPlmgQiZvsH8d14Zc5YyM I have no idea how the _token is generated it has a different length than the other tokens. I can see that same token when I look to the Modules Manager tab in admin, in the URL is the same token. What am I doing wrong? I am so frustrated I am trying to solve this for 2 days and no hope. Please tell me where I messed up.

Here is the routes.yml

image_route:
    path: STLuploader/images
    methods: [POST, GET]
    defaults:
      _controller: 'STLuploader\Controller\ImageSTLController::getimagesAction'
test_route:
    path: STLuploader/test
    methods: [POST, GET]
    defaults:
      _controller: 'STLuploader\Controller\testController::testAction'

Here is the controller:

<?php

namespace STLuploader\Controller;

use PrestaShop\PrestaShop\Adapter\Entity\Tools;
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use Symfony\Component\HttpFoundation\JsonResponse;

class ImageSTLController extends FrameworkBundleAdminController
{
    public function getimagesAction(){

        $id_product = Tools::getValue("product_id");
        $response = new JsonResponse($id_product);
        return $response;
    }
}

composer.json:

{
    "name": "jiri-svitil/stluploader",
    "description": "STL uploader and management module",
    "authors": [
        {
            "name": "Jiří Svítil",
            "email": "[email protected]"
        }
    ],
    "require": {
        "php": ">=5.6.0"
    },
    "autoload": {
        "psr-4": {
            "STLuploader\\Controller\\": "src/Controller"
        }
       
    },
    "config": {
        "prepend-autoloader": false
    },
    "type": "prestashop-module",
    "author": "Jiří Svítil",
    "license": "GNU GPL v2"
}

Prestashop version: 1.7.5 Thank you so much for your time.


Solution

  • So, first of all, I want to say that I hate the developer of Prestashop who didn't put this into documentation. Second is my solution: If you make admin controller that has route defined in Symfony, you just need to know what is the path name, in my case it is "image_route". Than you need to get the Symfony router instance. You can do that by adding this to top of your file use PrestaShop\PrestaShop\Adapter\SymfonyContainer; and to get the router instance $router = SymfonyContainer::getInstance()->get('router');. After that you can use this function to generate the URL that is created from the path name: $router->generate("image_route"). That is all.

    If somebody wants to help with this I am glad to do so. I think this is against basic human rights to finding this on your own.