Blog navigation keyboard_arrow_down

Blog Rss rss_feed

How to Create a New Admin Panel Controller in a PrestaShop Module: A Simple and Clear Guide

How to Create a New Admin Panel Controller in a PrestaShop Module: A Simple and Clear Guide

PrestaShop continues to evolve actively, making the system more convenient and functional for both users and developers. One significant innovation has been the new approach to creating admin panel module controllers using Symfony. In this article, we will explore how to correctly create such a controller in your module.

Creating the Controller Structure

First, create a new controller in your module’s directory. The file structure should be as follows:

namespace MyModule\Controller\Admin; use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController; use Symfony\Component\HttpFoundation\Response; class MyModuleAdminController extends FrameworkBundleAdminController { public function indexAction() { return $this->render('@Modules/mymodule/views/templates/admin/index.html.twig'); } }

Your controller should extend the FrameworkBundleAdminController class, which allows you to use Symfony functions in PrestaShop.

Setting Up Routes

To allow PrestaShop to access your new controller, you need to set up routes. This is done through the config/routes.yml file of your module:

mymodule_admin_index: path: /admin/mymodule controller: MyModule\Controller\Admin\MyModuleAdminController::indexAction methods: [GET]

Creating the Template for the Controller

Create a template for the admin page that will be displayed by your controller. This file should be located at views/templates/admin/index.html.twig:

{% extends '@PrestaShop/Admin/layout.html.twig' %} {% block content %} <h1>Welcome to the module admin panel!</h1> <p>Here you can manage your module’s settings.</p> {% endblock %}

Setting Up Controller Autoloading

Now you need to set up class autoloading for your module. To do this, add a composer.json file to the root of your module:

{ "name": "you/your-module", "description": "Description of your module", "autoload": { "psr-4": { "PrestaShop\\Modules\\MyModule\\": "src/" } }, "config": { "prepend-autoloader": false }, "type": "prestashop-module" }

Configuration Breakdown:

  • "name": "you/your-module" — specify the name of your module.
  • "autoload": { "psr-4": { "PrestaShop\\Modules\\MyModule\\": "src/" }} — this part of the configuration specifies autoloading for your controller located in the src/Controller/Admin directory. PrestaShop will automatically load classes from this namespace.
  • "config": { "prepend-autoloader": false } — this line disables the default PrestaShop autoloader so Composer can handle autoloading itself.

After creating the composer.json file, you need to generate the autoloader using the Composer command:

php composer.phar dump-autoload

This will create the vendor/autoload.php file that PrestaShop will use to correctly load your controller.

How to Test the URL for the New Controller

After creating the new admin panel controller in your module, it's important to ensure that the route (URL) is configured correctly and works as expected. To do this, perform a simple check.

Depending on your route configuration, the URL for your controller might look like this:

https://your-shop.com/admin-dev/index.php/admin/mymodule

Open this URL in your browser and verify that the page loads correctly. In our example, you should see the following text:

Welcome to the module admin panel! Here you can manage your module’s settings.

If the page doesn’t load, check the following:

  • Is the route configuration in your module’s config/routes.yml file set up correctly?
  • Is the template file located in the correct directory and free from errors?

Creating a new controller for the PrestaShop admin panel using Symfony gives developers more flexibility and customization options. Don’t forget to configure autoloading correctly via Composer and verify your module's routes.

Leave comments on whether everything was clear and what you think about the new admin panel controller in PrestaShop!

Download an example module

Was this blog post helpful to you?

    
No comments at this moment
close

Checkout

close

Favourites