【问题标题】:Creating and accessing custom module's REST routes in Prestashop 1.7.5在 Prestashop 1.7.5 中创建和访问自定义模块的 REST 路由
【发布时间】:2020-05-07 10:40:41
【问题描述】:

我正在尝试在我的 Prestashop 1.7.5 模块中创建一个自定义控制器。

我创建了一个自定义控制器:

# /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();
    }
}

我用我的composer.json 文件加载了这个类:

# /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": ""
}

我在模块的 routes 文件夹中添加了一条路由

# /var/www/html/modules/Profit/config/routes.yml

update_price_cut:
    path: Profit/price-cut
    methods: [GET]
    defaults:
      _controller: 'Profit\Controller\ProductProfitController::test'

但我不知道如何访问该路线。我试过了:

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

这些都不起作用。每一个都会导致 404 错误。

这是为模块的自定义控制器创建路由的正确方法吗?我该如何解决这个问题?

注意:此控制器应该是 BackOffice 控制器。我想用它来更新默认 PrestaShop 产品列表中的产品详细信息。

【问题讨论】:

    标签: php symfony composer-php prestashop prestashop-1.7


    【解决方案1】:

    现有的答案对我没有多大帮助,而且它没有提到实际的 URL,因为人们通过谷歌在这里磕磕绊绊。

    设置

    # /config/routes.yml
    
    my_route_name:
      path: /my_project/my_path # Leading / can be omitted
      methods: [GET]
      defaults:
        _controller: 'Me\MyProject\Admin\Controllers\MyController::indexAction' # This can point to any class and any public method.
    
    // my_project/admin/controllers/MyController.php
    
    class MyController extends FrameworkBundleAdminController
    {
        public function indexAction(): string 
        {
            return 'hello';
        }
    }
    

    然后我沿着相同的路径尝试找出 URL,最终到了这里。

    实际控制器网址

    由于某种原因,我的任何管理控制器中都不存在另一个答案中提到的方法generateUrl。我查看并发现它是在 Symfony 特征中定义的。它本质上是这样做的:

    $this->container->get('router')->generate('my_route_name', [], UrlGeneratorInterface::ABSOLUTE_PATH);
    

    最终返回了工作 URL:

    /admin1/index.php/modules/my_project/my_path?_token=...
    

    希望这对其他人有帮助。

    【讨论】:

      【解决方案2】:

      在管理控制器中尝试 $this->generateUrl('update_price_cut')。它将生成到您的控制器的正确路由。或者,如果您在不同的地方需要它,您可以创建自己的服务并使用它。更多信息您可以找到here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-09-28
        • 1970-01-01
        • 1970-01-01
        • 2022-01-08
        • 1970-01-01
        • 2014-03-17
        • 2019-09-03
        相关资源
        最近更新 更多