Controller does not define Model object

Dear all,

I am quite New to CakePHP, especially version 4.x.

I created Migrations and run migrate to create my DB tables, then I use bake to generate all the scaffolding files.

I have users and user_groups tables. users table has user_group_id foreign key.

Whenever i go to <app_root>/usergroups/ , or any actions of UserGroupsController , debug says:

[ Notice (1024)]: Undefined property: UserGroupsController::$UserGroups

I am stuck at this, I have read again convention, but everything seem to be fine. What did I do wrong?
I have wrote an app in CakePHP 3x before, so the concept of CakePHP is not too new to me, but obviously there is something wrong with my code.

I am currently have mental block because of this in last few days. Please someone point me out of this.
Thank you very much.

Could you post your UserGroupsController code here?

1 Like

Firstly thank you Mr. dakota for your kind and quick response.

Please see my UserGroupsController code below.

I have generated code in CakePHP 3.8 using bake, and the code works fine if I access with URL like <app_root>/user_groups/* and failed as above question if accessed with URL <app_root>/usergroups/* .

Tried again <app_root>/user_groups/* with CakePHP 4.0.1 , it says Cake\Http\Exception\MissingControllerException

Documentation said that we can access it either way (ver 3.x doc), but seem only the way with underscore works (in ver 3.x only). Is it a bug?

<?php
declare(strict_types=1);
namespace App\Controller;

class UserGroupsController extends AppController
{


    public function index()
    {
        $this->paginate = [
            //'contain' => ['Leaders', 'UpperGroups'],
        ];
        $userGroups = $this->paginate($this->UserGroups);

        $this->set('UGController', compact($this));
        $this->set(compact('userGroups'));
    }

   
    public function view($id = null)
    {
        $userGroup = $this->UserGroups->get($id, [
            'contain' => ['Leaders', 'UpperGroups', 'Users'],
        ]);

        $this->set('userGroup', $userGroup);
    }

   
    public function add()
    {
        $userGroup = $this->UserGroups->newEmptyEntity();
        if ($this->request->is('post')) {
            $userGroup = $this->UserGroups->patchEntity($userGroup, $this->request->getData());
            if ($this->UserGroups->save($userGroup)) {
                $this->Flash->success(__('The user group has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The user group could not be saved. Please, try again.'));
        }
        $leaders = $this->UserGroups->Leaders->find('list', ['limit' => 200]);
        $upperGroups = $this->UserGroups->UpperGroups->find('list', ['limit' => 200]);
        $this->set(compact('userGroup', 'leaders', 'upperGroups'));
    }

    
    public function edit($id = null)
    {
        $userGroup = $this->UserGroups->get($id, [
            'contain' => [],
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $userGroup = $this->UserGroups->patchEntity($userGroup, $this->request->getData());
            if ($this->UserGroups->save($userGroup)) {
                $this->Flash->success(__('The user group has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The user group could not be saved. Please, try again.'));
        }
        $leaders = $this->UserGroups->Leaders->find('list', ['limit' => 200]);
        $upperGroups = $this->UserGroups->UpperGroups->find('list', ['limit' => 200]);
        $this->set(compact('userGroup', 'leaders', 'upperGroups'));
    }

    
    public function delete($id = null)
    {
        $this->request->allowMethod(['post', 'delete']);
        $userGroup = $this->UserGroups->get($id);
        if ($this->UserGroups->delete($userGroup)) {
            $this->Flash->success(__('The user group has been deleted.'));
        } else {
            $this->Flash->error(__('The user group could not be deleted. Please, try again.'));
        }

        return $this->redirect(['action' => 'index']);
    }
}

Ah, I see the issue. CakePHP 4 changed the default routing to be dashed instead of underscored, so the correct url would be /user-groups.

It depends on your router configuration.