I am trying to create a custom controller in my Prestashop 1.7.5 module.
I created a custom controller:
# /var/www/html/modules/Profit/src/controller/ProductProfitController.php
namespace Profit\Controller;
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use Symfony\Component\HttpFoundation\JsonResponse;
class ProductProfitController extends FrameworkBundleAdminController {
public function test() {
return JsonResponse();
}
}
I loaded the class with my composer.json
file:
# /var/www/html/modules/Profit/composer.json
{
"name": "company/profit",
"description": "Moduł opłacalności",
"authors": [
{
"name": "Name",
"email": "Email"
}
],
"require": {
"php": ">=5.6.0"
},
"autoload": {
"psr-4": {
"Profit\\Controller\\": "src/controller/"
},
"classmap": [
"Profit.php",
"src/"
],
"exclude-from-classmap": []
},
"config": {
"preferred-install": "dist",
"prepend-autoloader": false
},
"type": "prestashop-module",
"author": "Name",
"license": ""
}
I added a route in my module's routes
folder
# /var/www/html/modules/Profit/config/routes.yml
update_price_cut:
path: Profit/price-cut
methods: [GET]
defaults:
_controller: 'Profit\Controller\ProductProfitController::test'
Yet I do not know how to access that route. I tried:
localhost:8001/admin-dev/Profit/price-cut
localhost:8001/modules/Profit/price-cut
localhost:8001/modules/Profit/Profit/price-cut
localhost:8001/Profit/price-cut
None of these work. Every single one of them leads to a 404 error.
Is this the proper way of creating routes to your module's custom controller? How can I fix this?
NOTE: This controller is supposed to be a BackOffice controller. I want to use it to update products' details from the default PrestaShop product list.
Try $this->generateUrl('update_price_cut') within admin controllers. It will generate a correct route to your controller. Or if you need it in a different place you can create own service and use it. More information you can find here