bradley546994
bradley546994

Reputation: 630

Cakephp 3 component and model has the same name

because my component, controller and model has the same name:

<?php
namespace Plug\Controller;

use Plug\Controller\AppController;

class SettingController extends AppController
{

    public function initialize(){
        parent::initialize();
        $this->loadModel('Setting');
        $this->loadComponent('Plug.Setting');

    }

How do I know how to refer to component or model ?

Upvotes: 2

Views: 658

Answers (1)

floriank
floriank

Reputation: 25698

Check the manual, almost everything is there. Please consider checking the documentation, it's there to be read.

Aliasing Components

One common setting to use is the className option, which allows you to alias components. This feature is useful when you want to replace $this->Auth or another common Component reference with a custom implementation:

// src/Controller/PostsController.php
class PostsController extends AppController
{
    public function initialize()
    {
        $this->loadComponent('Auth', [
            'className' => 'MyAuth'
        ]);
    }
}

// src/Controller/Component/MyAuthComponent.php
use Cake\Controller\Component\AuthComponent;

class MyAuthComponent extends AuthComponent
{
    // Add your code to override the core AuthComponent
}

Upvotes: 1

Related Questions