Add a Basic Controller in Oro Platform Application

Posted About 5 years ago. Visible to the public.

After we have our bundle, we can add a controller with just 2 files.

Add a Controller Class

Add file src\DbugBundle\Controller\DbugController.php

<?php
namespace DbugBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;


/**
 * @Route("/dbug")
 */
class DbugController
{
    /**
     * @Route("/phpinfo", name="dbug_phpinfo")
     */
    public function phpinfoAction(): Response
    {
        ob_start();
        phpinfo();
        $html = ob_get_contents();
        ob_get_clean();

        return new Response('<html><body>'.$html.'</body></html>');
    }
}

The URL is oro.mis.sc/index_dev.php/dbug/phpinfo.

See route doc Show archive.org snapshot .

Add the Routing Information

Add file src\DbugBundle\Resources\config\oro\routing.yml.

dbug_bundle:
    resource: "@DbugBundle/Controller"
    type: annotation

That's it. Now, for the route to be discovered, we need to warm up the cache with

[kiat@reporting misoro]$ sudo -u nginx php bin/console cache:clear

 // Clearing the cache for the dev environment with debug
 // true

 [OK] Cache for the "dev" environment (debug=true) was successfully cleared.

Image

Get Info on a Route

[kiat@reporting misoro]$ sudo -u nginx php bin/console debug:router dbug_phpinfo
+--------------+---------------------------------------------------------+
| Property     | Value                                                   |
+--------------+---------------------------------------------------------+
| Route Name   | dbug_phpinfo                                            |
| Path         | /dbug/phpinfo                                           |
| Path Regex   | #^/dbug/phpinfo$#sD                                     |
| Host         | ANY                                                     |
| Host Regex   |                                                         |
| Scheme       | ANY                                                     |
| Method       | ANY                                                     |
| Requirements | NO CUSTOM                                               |
| Class        | Symfony\Component\Routing\Route                         |
| Defaults     | _controller: DbugBundle:Dbug:phpinfo                    |
| Options      | compiler_class: Symfony\Component\Routing\RouteCompiler |
| Callable     | DbugBundle\Controller\DbugController::phpinfoAction     |
+--------------+---------------------------------------------------------+

More info Show archive.org snapshot .

Add ACL to Action

<?php
namespace DbugBundle\Controller;

use Oro\Bundle\SecurityBundle\Annotation\Acl;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;


/**
 * @Route("/dbug")
 */
class DbugController
{
    /**
     * @Route("/phpinfo", name="dbug_phpinfo")
     * @Acl(
     *     id="dbug_phpinfo",
     *     type="action",
     *     label="Dbug phpinfoAction"
     * )
     */
    public function phpinfoAction(): Response
    {
        ob_start();
        phpinfo();
        $html = ob_get_contents();
        ob_get_clean();

        return new Response('<html><body>'.$html.'</body></html>');
    }
}

We can test it by disable the access in System > Roles > Administrator > Edit > Capabilities or Account Management:

Image

Once the config is saved, we are served a 403:

Image

Pass a Class as Argument to Controller Action

Instead of creating a new class in the action, we can pass the class as an argument by autowirng the class. So we can pass class DbugFile

use DbugBundle\Util\File as DbugFile;
// ...

/**
 * @Route(
 *  "/tail/{lines}/{filename}",
 *  name="dbug_tail",
 *  defaults={"lines"=20, "filename"="dev.log"},
 *  requirements={"filename"=".+"}
 * )
 * @Acl(
 *     id="dbug_tail",
 *     type="action",
 *     label="Dbug tailAction"
 * )
 */
public function tailAction($lines, $filename, DbugFile $file, KernelInterface $kernel): Response
{
    $path = $filename[0] == '/'
        ? $filename
        : (strpos($filename, '/')
            ? $kernel->getRootDir() . '/'. $filename
            : $kernel->getLogDir() . '/' . $filename
        );
    $lines = $lines ?: 20;
    $output = $file->tail($path, $lines);
    $output = "<h3>$path <em>last $lines lines</em></h3><pre>".print_r($output, true).'</pre>';
    return new Response('<html><body>'.$output.'</body></html>');
}

So, there is no need to do $file = new DbugFile.

404 or 500 Errors in Oro Production

If we get 404 page not found error with URLs without the index_dev.php, or a 500 internal server error, and in var/logs/prod.log:

request.CRITICAL: Uncaught PHP Exception RuntimeException: "Controller ... requires that you provide a value for the "$ocr" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one...

we need to delete the prod cache and then warm it up in prod environment:

[kiat@reporting misoro]$ sudo rm -rf var/cache/prod/*
[kiat@reporting misoro]$ sudo -u nginx php bin/console cache:clear -e prod
kiatng
Last edit
Over 4 years ago
kiatng
Keywords
BAP
Posted by kiatng to Oro (2019-03-14 02:26)