Symfony3.4 and Drupal console commands reference

php bin/console list
php bin/console generate:controller

Symfony console:


What we are going to do?
1. Install symfony at http://127.0.0.1:8001
2. Create a controller at http://127.0.0.1:8001/products
3. Create a controller at http://127.0.0.1:8001/product

Installing symfony:

1. apache-doc-root> composer create-project symfony/framework-standard-edition autotrader2

2. autotrader2> php bin/console server:run

It should show like

 [OK] Server listening on http://127.0.0.1:8001 

3. Run in browser http://127.0.0.1:8001

It should say

Welcome toSymfony 3.4.4

Create a controller at http://127.0.0.1:8001/products

Generate bundle:

1. autotrader2> php bin/console generate:bundle
Note:
a) Give bundle name as ProductBundle
b) If following error occurs
Checking that the bundle is autoloaded
FAILED
then add  "ProductBundle\\": "src/ProductBundle" in composer.json . So it should look like this

"psr-4": {
            "AppBundle\\": "src/AppBundle",
            "ProductBundle\\": "src/ProductBundle"
        },
then run "composer dump-autoload" 

Generate controller:

 2. autotrader2> php bin/console generate:controller --controller=ProductBundle:Products --actions=productsAction:/products:@Product:Products:products.html.twig --no-interaction

Output:
 Controller generation       

  created ./src/ProductBundle/Resources/views/Products/
  created ./src/ProductBundle/Resources/views/Products/products.html.twig
  created ./src/ProductBundle/Controller/ProductsController.php
  created ./src/ProductBundle/Tests/Controller/
  created ./src/ProductBundle/Tests/Controller/ProductsControllerTest.php
Generating the bundle code: OK


in ./src/ProductBundle/Controller/ProductsController.php replace
     return $this->render('@Product:Products:products.html.twig');
with
     return $this->render('@Product\Products\products.html.twig', array());

remove following line in ./src/ProductBundle/Resources/views/Products/products.html.twig
{% extends "::base.html.twig" %}
from products.html.twig

Now visit
http://127.0.0.1:8001/products
It should say
ProductBundle:Products:products

Welcome to the Products:products page

Create a controller at http://127.0.0.1:8001/product

1. autotrader2> sudo php bin/console generate:controller --controller=ProductBundle:Product --actions=productAction:/product:@Product\Product\product.html.twig --no-interaction

[sudo] password for me:


  Output:

Controller generation               

  created ./src/ProductBundle/Resources/views/Product/
  created ./src/ProductBundle/Resources/views/Product/product.html.twig
  created ./src/ProductBundle/Controller/ProductController.php
  updated ./src/ProductBundle/Tests/Controller/ProductControllerTest.php
Generating the bundle code: OK


replace
return $this->render('@Product:Default:index.html.twig');
with
return $this->render('@Product\Index\product.html.twig', array());

remove following line
{% extends "::base.html.twig" %}
from product_index.html.twig

Now visit
http://127.0.0.1:8001/product
It should say

ProductBundle:Products:product

Welcome to the Product:product page


Drupal console:

drupal-doc-root> drupal generate:module  --module="custommodule"  --machine-name="custommodule"  --module-path="modules/custom"  --description="My Awesome Module"  --core="8.x"  --package="Custom"  --module-file  --composer  --test  --twigtemplate

drupal-doc-root> drupal generate:controller  \
  --module="custommodule"  \
  --class="ProductController"  \
  --routes='"title":"Index", "name":"custommodule.product_controller_index", "method":"index", "path":"/product"'  \
  --test

note: if the output says "Path "modules/custom/custommodule/src/Controller" is invalid. You need to provide a valid path. " then use sudo infront of the command.

If you already have the module and want to generate a event subscriber
hello> drupal generate:event:subscriber  --module="hello"  --name="hello.default"  --class="DefaultSubscriber"  --events='kernel_request'
Notes:
Dont add any services during generation process unless it is necessary
The getSubscribedEvents should look like this.
static function getSubscribedEvents() {
   $events['kernel.response'] = ['kernel_request'];
    return $events;
  }

To create a block:
drupal generate:plugin:block --module="dtc" --class="DTCBlock" --label="DTC block" --plugin-id="dtc_block" --theme-region="header"






Comments

Popular posts from this blog

Programatically create layout builder section in Drupal

Code quality analysis of Drupal. Can I use Sonar?

Set up Drupal7 to Drupal8 migration in simple steps (using Drush)