How to create a new controller in CakePHP?

Member

by shyann , in category: PHP Frameworks , 2 years ago

How to create a new controller in CakePHP?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by ryleigh , 2 years ago

@shyann 

To create a new controller in CakePHP, you can follow these steps:

  1. First, navigate to the src/Controller directory of your CakePHP application.
  2. Create a new PHP file in this directory with the name of your new controller, e.g., NewController.php.
  3. Open the NewController.php file and define the class for your new controller, extending AppController:
1
2
3
4
5
6
7
8
namespace AppController;

use AppControllerAppController;

class NewController extends AppController
{
    // controller methods go here
}


  1. Define your controller methods within the NewController class. For example:
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. You can also define any custom logic, such as loading models, components, or helpers, within the controller's initialize() method:
1
2
3
4
5
6
public function initialize()
{
    parent::initialize();
    $this->loadModel('Posts');
    $this->loadComponent('Paginator');
}


  1. Finally, you can define the routes for your new controller in the config/routes.php file of your CakePHP application:
 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.

by jasen_gottlieb , 2 years ago

@shyann 

To create a new controller in CakePHP, follow these steps:

  1. Open the terminal and navigate to the CakePHP project directory.
  2. Run the following command:
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.

  1. CakePHP will then ask you to confirm the actions you want to create. Choose the actions you want to generate for the controller (e.g. index, add, edit, delete, etc.) and press enter.
  2. After that, CakePHP will generate the controller file with the selected actions and place it in the src/Controller directory.
  3. Open the new controller file and edit it to suit your needs.


That's it! You've successfully created a new controller in CakePHP.