[CakePHP]: Tips for add more a Component in CakePHP


Posted on 2015-11-23 09:31:50

Component is the extension of  Framework. Allows users to customize and use it flexibly. Such as a useful component in CakePHP which we used as: ACL (decentralization), mail, time, security, ...

Also, to perform specific purposes. CakePHP also allows users to embellish or a component written specifically to cater to your application.

To use existing components in  , the controller we declare:

Suppose I want to remove Vietnamese sign posts that I received from a user, not just the commentary on product that includes commentary about the project, about a certain picture in my website ... How to book 1 Class Component name:


Component I want to create named Common , respectively classCommonComponent and corresponding file is common.php (app / controllers / components / common.php) File Content common.php as follows:


However, code can run, but you will get the following error message:

Warning (2): call_user_func_array() [function.call-user-func-array]:…

Much less you use some shared components on the network and will also experience such a problem. But while the author does not mention how to fix it, perhaps in part they are basic knowledge we have mastered But okay, for the following way:
Add your Component Class line of code below thick :

So my complete Common Component as follows:
( app / controllers / components / common.php )

Common Component using the newly created class:
I create 1 Controller name Testcommons (app / controllers / testcommons _controller.php) use the newly created class Common COMONENT

Tạo file test_component.ctp (app/views/testcommons/ test_component.ctp)

Test: http: // localhost / cakephp / testcommons / test_component Note: The $ components are declared in controllers are only available in controllers that if I declare in the NewsController Controller to Controller Product pages, use $ this- > Common-> unicode_convert () will get an error immediately! Thus inapplicable nature "used anytime, anywhere".

But okay, we can solve this problem by:
- Creating file app_controller.php placed in app directory, the file contents are as follows:

Everything put in AppController will work on all other controllers, so we just declare