namespace MyApp\Controller; use Cake\Controller\Controller; class ProductsController extends Controller { public function index() { // your code here } }
namespace MyApp\Controller; use Cake\Controller\Controller; class ArticlesController extends Controller { public function initialize() { parent::initialize(); $this->loadComponent('Paginator'); } public function index() { $articles = $this->Paginator->paginate($this->Articles->find()); $this->set(compact('articles')); } }In this example, we create an ArticlesController which also extends the base Controller class. However, we have added an initialize method which is called automatically before any action in the controller. Inside the initialize method, we load the Paginator component. This component is used in the index method to paginate a collection of articles. Both examples use the constructClasses method indirectly. The CakePHP framework automatically calls this method when creating instances of controller classes, allowing them to easily load and manage their own dependencies. The package library being used is the CakePHP framework itself.