Friday 3 July 2015

How to use plug-in's component in our application controller - CakePHP 3 0

Here is a small How to about creating and using plugin component in application controller.

Here we are going to create 'TestingComponent' inside of 'Test' plugin, Then get 'Hello world' text from component function and print it from our application's controller.

first create 'TestComponent' class in 'YourCakeDirectory\Plugins\Test\src\Controller\Component\TestComponent.php'

Here is the most important thing we have to do with our plugin load in CakePHP bootstrap. Before calling plug-in component in application controller, we need to auto load it in CakePHP. If our plugin created using bake method, we should autoload this class in 'composer.json'. If our plugin was manually created, then we should use 'autoload' parameter as a value 'true' as a configuration parameter while loading this plugin in application's 'YourCakeDirecctory\Config\bootstrap.php'.
The bellow one is a way to load component in controller on the fly.
Now we are going to load plugin's component. So we need to use our plugin component name followed by plugin name and dot('.'). Here we are going to load 'TestingComponent' from the plugin 'Test'. So, the code will be like the bellow,

Now we will get the output of plugin's component string 'Hello World' which is echoed from our application's controller.

Have any doubt, feel free to comment here!

0 comments:

Post a Comment