Create plugin cakephp and use it in browser

431 Views Asked by At

I'm trying to create a plugin, this is my folder layout;

Folder layout

in bootstrap.php

CakePlugin::load('ContactManager');

in routes.php

Router::connect(
    '/ct/', 
    array(
        'plugin' => 'ContactManager', 
        'controller' => 'Contacts', 
        'action' =>'index'
    )
);

ContactManagerAppController.php

<?php
class ContactManagerAppController extends AppController {}

ContactsController.php

<?php
class ContactsController extends ContactManagerAppController {
    public $uses = array('ContactManager.Contact');

    public function index() {
        //...
    }
}

ContactManagerAppModel.php

<?php
class ContactManagerAppModel extends AppModel {}

Contact.php

class Contact extends ContactManagerAppModel {}

How do I display index.ctp in my browser index.ctp

<?php echo 'hello'; ?>

Requesting http://localhost/cakephp/ct/ContactManager/Contacts gives a missing controller error, instead of my plugin controller index:

404 for plugin url request

2

There are 2 best solutions below

2
On

You should be able to access your plugin's Contacts Controller (without setting up routes) from /contact_manager/contacts. So by the looks of your question the absolute URL should be:-

http://localhost/cakephp/contact_manager/contacts

If this is not working then there's something wrong with the plugin's setup. Make sure the plugin files are readable and that the filenames and classes are all correct. Your plugin structure looks good otherwise.

If the above URL works then (and only then) you can consider re-routing it. To do this you need to do something like this (preferably in /app/Config/routes.php):-

Router::connect(
    '/ct/', 
    array(
        'plugin' => 'contact_manager', 
        'controller' => 'contacts', 
        'action' =>'index'
    )
);

If the route does not work try checking that nothing is overridding it elsewhere in your routes file.

You should use snake case (e.g. 'contact_manager') not camel case (e.g. 'ContactManager') in your route parameters.

1
On

This is my answer ---> thanks all https://i.stack.imgur.com/JHXQG.jpg

in Controller:

AppController.php
ContactsController.php

in Model

AppModel.php
Contact.php

in View/Contacts

index.ctp

app/Config/routes.php

Router::connect('/ct', array('plugin'=>'ContactManager', 'controller' => 'contacts', 'action' => 'index'));

app/Config/bootstrap.php

CakePlugin::load(array('ContactManager' => array('bootstrap' => true, 'routes' => true) ));