welson
welson

Reputation: 109

Overrides controller form Prestashop 1.7.7

Please, does anyone have an example of Admin controller override for Prestashop 1.7.7 (based on symfony)

Thank you for your answers.

Upvotes: 1

Views: 2893

Answers (1)

Skaparate
Skaparate

Reputation: 489

I created a sample module for this, which can be download from here (under Releases).

The process to override the specific route /adminXXXX/index.php/sell/orders/ is this:

  1. You create a Symfony controller in your project (the code is a little further down). I create them inside src/Controller, which is the convention. This controller must extend the class PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController. I load my classes using composer auto loader, so here's the setup:
"autoload": {
  "psr-4": {
    "Skaparate\\": "src/"
  },
  "classmap": [
    "sk_testmodule.php"
  ],
  "exclude-from-classmap": []
},
  1. You need to create a folder config in the root of your project. This is important because PrestaShop will load the routes and services from there.
  2. Create a file called routes.yml inside the previously created folder.
  3. Inside routes.yml define the route you want to override. In this case, the route is called admin_orders_index, hence we write this:
   admin_orders_index:
     path: /sell/orders/
     # POST is required because admin_order_index is also setup as the grid reset return route.
     methods: [GET,POST]
     defaults:
        _controller: 'Skaparate\Controller\Admin\CustomOrdersController::indexAction'
       _disable_module_prefix: true
  1. And finally, this is the actual Controller:
<?php

namespace Skaparate\Controller\Admin;

if (!defined('_PS_VERSION_')) {
    die;
}

use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use PrestaShopBundle\Security\Annotation\AdminSecurity;
use PrestaShopBundle\Security\Annotation\ModuleActivated;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;

class CustomOrdersController extends FrameworkBundleAdminController
{
    /**
     * Shows list of orders
     *
     * @param Request $request
     * @param OrderFilters $filters
     *
     * @return Response
     */
    public function indexAction(Request $request)
    {
        return $this->render(
            '@Modules/sk_testmodule/views/templates/admin/sell/order/index.html.twig',
            [
                'enableSidebar' => true,
            ]
        );
    }
}

This project structure would be:

├── composer.json
├── composer.lock
├── config
│   └── routes.yml
├── README.md
├── sk_testmodule.php
├── src
│   └── Controller
│       └── Admin
│           └── CustomOrdersController.php
└── views
    └── templates
        └── admin
            └── sell
                └── order
                    └── index.html.twig

Of course, you would need to create the file views/templates/admin/sell/orders/index.html.twig. I put it in that path to maintain PrestaShop structure, but it's not required.

For reference, Symfony routes are defined in src/PrestaShopBundle/Resources/config/routes (relative to the PrestaShop installation).

Regards!

Upvotes: 4

Related Questions