How to access plugin in cakephp - cakephp

I am new in cakephp, I want to call the plugin via the URL. Here is URL of
http://testproject.local/PluginName/ControllerName/ActionName
When I Run this URL at that time I found "Missing Controller" error.
Missing Controller
Error: <ControllerName>Controller could not be found.
Error: Create the class <ControllerName>Controller below in file: `app/Controller/<ControllerName>Controller.php`
It's showing me
`Exception Attributes: array ( 'class' => 'PracticeFusionController', 'plugin' => NULL, )`
Here is my routes.php
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
/**
* ...and connect the rest of 'Pages' controller's urls.
*/
Router::connect('/Pages/*', array('controller' => 'pages', 'action' => 'display'));
Router::connect('/gods/:action/*', array('plugin' => 'nova', 'controller' => 'gods'));
Router::connect('/gods', array('plugin' => 'nova', 'controller' => 'gods'));
Router::parseExtensions('json', 'xml');
Router::mapResources('events');
Router::connect('/<pluginName>', array('plugin' => '<pluginName>', 'controller' => '<ControllerName>'));

You can only load/open Plugin routes if you actually loaded the plugin in your project's bootstrap. You don't need to include a plugin route in your core app's routes.php. If you want to add plugin specific routes, you can load the plugin specific routes file, using the routes option. Note that by default all /plugin/controller/action routes are already routed properly, you don't need a separate routes file for that.
So, in your core app's app/Config/bootstrap.php, add:
CakePlugin::load('YourPlugin', array('routes' = true));
The routes from your plugin's Config/routes.php will then be loaded and can be used.
More details on this can also be found in the documentation.

If above solutions does not works, please load plugin like CakePlugin::load('YourPlugin', array('routes' => true)); Please note array passed is : array('routes' => true)

Related

Admin index route ignored w/ CakePHP 2.1

I'm working on a CakePHP 2.1 project and I have an issue concerning the "homepage" for admin panel.
When I enter : mysite.com/admin
I have a message telling me "AdminController cannot be found".
I declared this route in config/routes.php :
Router::connect('/admin', array('controller' => 'mycontroller', 'action' => 'index', 'admin' => true));
And when I enter mysite.com/admin/mycontroller as URI, it works.
Do you have an idea ?
Thank you in advance.
Edit | My routes.php file :`
/**
* Here, we are connecting '/' (base path) to controller called 'Pages',
* its action called 'display', and we pass a param to select the view file
* to use (in this case, /app/View/Pages/home.ctp)...
*/
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
/**
* ...and connect the rest of 'Pages' controller's URLs.
*/
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
/**
* Load all plugin routes. See the CakePlugin documentation on
* how to customize the loading of plugin routes.
*/
CakePlugin::routes();
/**
* Load the CakePHP default routes. Only remove this if you do not want to use
* the built-in default routes.
*/
require CAKE . 'Config' . DS . 'routes.php';
// Custom routes
Router::connect('/admin', array('controller' => 'jobapplications', 'action' => 'index', 'admin' => true));
As suspected, there are conflicting routes, namely the default routes provided by the core, which you are including via
require CAKE . 'Config' . DS . 'routes.php';
before defining your /admin route.
The defaults connect various routes that can cloak yours, for example:
Router::connect("/{$prefix}/:controller", $indexParams);
or
Router::connect('/:controller', array('action' => 'index'));
Long story short, order matters, move your route definition above the inclusion of the default routes.

CakePHP 3: Plugin routing without a slash at the end?

Admin plugin / PagesController methods:
home
index
add
..
removed default display method.
Problem, i can't access url without slash at end mysite.com/admin/pages , if i try get redirect to mysite.com/admin/webroot/pages and error message
Error: WebrootController could not be found.
For all other Controllers url without slash at end works.
Router in admin plugin / config:
Router::plugin('Admin', function ($routes) {
$routes->connect('/login', ['controller' => 'Users', 'action' => 'login']);
$routes->connect('/new-password', ['controller' => 'Users', 'action' => 'newPassword']);
$routes->connect('/reset-password', ['controller' => 'Users', 'action' => 'resetPassword']);
//$routes->connect('/pages', ['controller' => 'Pages', 'action' => 'index']);
$routes->connect('/', ['controller' => 'Pages', 'action' => 'home']);
$routes->fallbacks('DashedRoute');
});
This thread on github may help you.
For what I know, the only way is to set the document root to /webroot
For anyone interested on changing the document root for the primary domain on cPanel: check here

CakePHP routing to controller and page

Is it possible to identify a controller and pagename in the same url
Router::connect('/:controller/');
Router::connect('/:pagename/', array('controller' => 'home','action'=>'index'));
www.example.com/controller
so that the controller goes to the :controller/index
www.example.com/pagename
so that the page goes to home/index
Its really confusing what really you wants. If a url ends with :controller like www.example.com/posts its generally map index action. Now if the url ends with pagename, means action, like www.example.com/mypage, you can map that as-
Router::connect('/mypage', array('controller' => 'homes', 'action' => 'index'));
So when a user browse www.example.com/mypage, it will map to HomesController and index action.
Try following code for pagename:
Router::connect('/:slug', array('controller' => 'home', 'action' => 'index'));
UPDATE 1
If you want to treat www.example.com/user as same as www.example.com/users then you need to add following, because CakePHP controller name is plural as per CakePHP naming convention:
Router::connect('/user/:action/*', array('controller' => 'users'));
UPDATE 2
Router::connect(':slug') overwrite all of default routing. So you need to use custom routing class as like as :
App::uses('SlugRoute', 'Routing/Route');
Router::connect(
'/:slug',
array('controller' => 'home', 'action' => 'index'),
array('routeClass' => 'SlugRoute')
);

Custom route configuration in cakephp

In route file there is a line like
Router::connect('/', array('controller' => 'admins', 'action' => 'login'));
I want to do something if anyone write a URL like http://abc.com/webroot or http://abc.com/css_or_js then it also goes to admin's login action. If so then what can i do then?
Router::connect('/webroot/*', array('controller' => 'admins', 'action' => 'login'));
Router::connect('/css/*', array('controller' => 'admins', 'action' => 'login'));
Router::connect('/js/*', array('controller' => 'admins', 'action' => 'login'));
but it works for webroot now and did not work for css or js folder or any other folder. Please help me in this matter. I will be very grateful to you.
The reason the css and js routes aren't working is because Cake's dispatcher is seeing them as an asset, so it skips the routing process entirely and delivers the asset. The only way around this, as I see, is to write a custom dispatcher.
You shouldn't be writing routes for the webroot directory and its folders anyway. The webroot folder should be the document root on your virtual host, and is therefore seen as the root of the site.

Problems with routes and CakePHP 2.2.2

I have installed CakePHP on windows over II7 and i am having problems with the routes.
I have created a Model, a Controller and a View for Users.
When i try to access the index view, i do it like this without any problem:
http://myhost/cakephp/users/
But, when I try to add a new user, the view doesn't load properly:
http://myhost/cakephp/users/add/
It shows this error:
Error: AddController could not be found.
Error: Create the class AddController below in file: app\Controller\AddController.php
In order to make it work, i have to do this in app/Config/routes.php:
Router::connect('/users/add', array('controller' => 'users', 'action' => 'add'));
But that wouldn't be necessary if it worked well.
Neither the delete or view views load.
What's going on? How can i detect the problem?
Thanks.
EDIT
Content of routes.php:
Router::connect('/', array(
'controller' => 'pages', 'action' => 'display', 'home'
));
Router::connect('/pages/*', array(
'controller' => 'pages', 'action' => 'display'
));
CakePlugin::routes();
require CAKE . 'Config' . DS . 'routes.php';
Ok, it seems i have solved it. It was all because of adding a routing prefix using cake bake console... I had to comment this line at core.php
Configure::write('Routing.prefixes', array('users'));

Resources