@shyann
To create a new controller in CakePHP, you can follow these steps:
1 2 3 4 5 6 7 8 |
namespace AppController; use AppControllerAppController; class NewController extends AppController { // controller methods go here } |
1 2 3 4 5 6 7 8 9 |
public function index() { // controller logic for index action } public function view($id) { // controller logic for view action } |
1 2 3 4 5 6 |
public function initialize() { parent::initialize(); $this->loadModel('Posts'); $this->loadComponent('Paginator'); } |
1 2 3 4 5 6 7 8 9 10 11 12 |
use CakeRoutingRouteBuilder; use CakeRoutingRouter; Router::plugin( 'NewPlugin', ['path' => '/new-plugin'], function (RouteBuilder $routes) { $routes->connect('/new', ['controller' => 'New', 'action' => 'index']); $routes->connect('/new/:id', ['controller' => 'New', 'action' => 'view']) ->setPass(['id']); } ); |
In the example above, we have defined the routes for a new plugin called NewPlugin
, and created two routes for the NewController
- one for the index
action and one for the view
action that accepts an id
parameter.
That's it! You should now be able to access your new controller and its actions via the defined routes.
@shyann
To create a new controller in CakePHP, follow these steps:
1
|
bin/cake bake controller <ControllerName> |
Replace <ControllerName>
with the name of your new controller, for example:
1
|
bin/cake bake controller Articles |
This command will create a new controller file ArticlesController.php
in the src/Controller
directory.
That's it! You've successfully created a new controller in CakePHP.