Cakephp 3.6 - Missing Controller Error - cakephp

I am having this error which does not seem to make sense. I am using this plugin: https://github.com/hashmode/cakephp-tinymce-elfinder. I am in need of creating and admin route. However, even though CakePHP sees the plugin, it does not see the Controller within it. I don't see what I am doing wrong.
This is my route for /admin/elfinder:
Router::prefix('admin', function ($routes) {
$routes->connect('/elfinder', ['plugin' => 'CakephpTinymceElfinder', 'controller' => 'Elfinders', 'action' => 'elfinder']);
});
This is the controller/action I am trying to access
https://github.com/hashmode/cakephp-tinymce-elfinder/blob/master/src/Controller/ElfindersController.php
But I get the following error:
2018-06-01 15:20:33 Error: [Cake\Routing\Exception\MissingControllerException] Controller class Elfinders could not be found.
Request URL: /admin/elfinder
It is definitely finding the Plugin. Why can't CakePHP find the controller?

According to the official CookBook you need to configure your prefixed routes in the following way. Hope this helps.
Router::plugin('YourPlugin', function ($routes) {
$routes->prefix('admin', function ($routes) {
$routes->connect('/:controller');
});
});
https://book.cakephp.org/3.0/en/development/routing.html#prefix-routing
Some time ago I wrote a plugin for my personal needs. I needed to bind its controllers to the /shop and /shop/api URLs. I managed to do it like this
Router::scope('/shop',['plugin'=>'MyPlugin'] ,function (RouteBuilder $routes) {
$routes->prefix('api', function($routes) {
$routes->extensions(['json']);
$routes->connect('/:controller');
$routes->resources('Categories');
$routes->resources('Products');
$routes->resources('Prices');
$routes->resources('Pricetypes');
});
$routes->connect('/:controller');
$routes->fallbacks(DashedRoute::class);
});

Related

CakePHP 3 - Controller not found when setup thru routes.php

Trying to play around with a test API. Based on the following routes setup, if I request /v1/tests/index.json I will get a JSON Object Response as expected, but if I request /v1/test/index.json I will get an error that TestController is missing. I have checked docs and I can't seem to figure out what is wrong. I expected the $routes->connect('/test', [...]); to work, but it is not. Any help in shining some light into this is appreciated.
<?php
use Cake\Core\Plugin;
use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;
Router::defaultRouteClass('DashedRoute');
Router::extensions(['json', 'xml']);
Router::scope('/', function (RouteBuilder $routes) {
$routes->prefix('v1', function (RouteBuilder $routes) {
$routes->connect('/test', ['controller' => 'Tests', 'action' => 'index']);
$routes->fallbacks('InflectedRoute');
});
$routes->fallbacks('DashedRoute');
});
Plugin::routes();
There is no explicit route set up matching /v1/test/index.json. Your:
$routes->connect('/test', ['controller' => 'Tests', 'action' => 'index']);
route will match /v1/test or /v1/test.json|xml, and that's all.
/v1/test/index.json will be catched by the fallback routes, and hence try to connect to the controller matching test, ie TestController.
Check out Cookbook > Routing > Connecting Routes more closely, you're doing what is shown in the /government example.
Did you try to specify the action in the route connect?
$routes->connect('/test/index', ['controller' => 'Tests', 'action' => 'index']);

CakePHP v3.x API prefixed routes

I have fallowing routes:
Router::scope('/', function (RouteBuilder $routes) {
Router::prefix('api', function ($routes) {
$routes->extensions(['json', 'xml']);
$routes->resources('JobChemicals');
$routes->fallbacks('DashedRoute');
});
Url: /api/job_chemicals/2.json - WORKS
Url: /api/job-chemicals/2.json - NOT
Action JobChemicalsController::2() could not be found, or is not accessible. Why?! I am using DashdRoute not underscored. Any ideas? I know it is small thinks, but it makes me crazy.
$routes->fallbacks('DashedRoute');
This only affects the fallback routes, not the call to $routes->resources.
Resource routes default to underscores
As noted in the question, underscored urls work.
As also noted in the docs, resource routes default to underscores:
By default, multi-worded controllers’ URL fragments are the underscored form of the controller’s name. E.g., BlogPostsController‘s URL fragment would be /blog_posts.
You can specify an alternative inflection type using the inflect option:
Router::scope('/', function ($routes) {
$routes->resources('BlogPosts', [
'inflect' => 'dasherize' // Will use ``Inflector::dasherize()``
];
});
The above will generate URLs styled like: /blog-posts/*.
The reason for this discrepancy is legacy (in 3.0 underscores were the default url inflection for everything), but the fix is simple - just include the inflect option to the resources call.

CakePhp Routing Interfering with AngularJS Routes

Okay so the problem I am having deals with using CakePhp (v3) and AngularJS. I want to use CakePhp purely to host my JSON webservices. I have this working and the routes prefixed with API. The problem I have is getting Html 5 routing to work with Angular. For instance, if I go to http://localhost/appname/home I want Angular to intercept this route not CakePhp. Currently, if I were to do this CakePhp will complain that the HomeController doesn't exist. I found this stack overflow post which allowed me to get Angular Hashtag routing functional/while maintaining the /api/* routes, but I really don't like to have my routes look like http://localhost/appname/#/home. I feel like there should be an .htaccess change I could make to allow Cake to handle the default URL path (ie: http://localhost/ thus opening by default home.ctp) but otherwise only look for http://localhost/api/* prefixed routes. I did try adding a RewriteRule ^/api/* with no luck unfortunately. I typically don't work in Apache/PHP in general so I am struggling to find the solution to this problem. Also, I made sure the rewrite module is enabled in my httpd.conf file. Here is my current routes.php file showing the API prefixing and the default routes to bring up home.ctp when hitting the root of the host. Any guidance from a more seasoned *AMP developer is much appreciated!
<?php
use Cake\Core\Plugin;
use Cake\Routing\Router;
Router::defaultRouteClass('Route');
Router::prefix('api', function ($routes) {
$routes->extensions(['json', 'xml']);
$routes->resources('Users');
$routes->resources('UserInfo');
Router::connect('/api/users/register', ['controller' => 'Users', 'action' => 'add', 'prefix' => 'api']);
Router::connect('/api/userinfo/create', ['controller' => 'UserInfo', 'action' => 'add', 'prefix' => 'api']);
$routes->fallbacks('InflectedRoute');
});
Router::scope('/', function ($routes) {
$routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
$routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
$routes->fallbacks('InflectedRoute');
});
Plugin::routes();
Maybe I'm misunderstanding your question, but by the time the request gets to the server, wouldn't it already be too late? If you want "angular to intercept" the route, the solution would need to be done client side and not in a .htaccess file. So you would have angular route that routes /appname/home to the correct file.

Laravel 5 routing not working when using slashes inside groups?

I am facing a little weird problem with a simple routing in Laravel 5. Below, I have to commented code snippets.
// It works when I call /tribut.updateStatus URL
$router->group(['prefix' => 'admin', 'before' => 'isLogged|isAdmin'], function ($router) {
$router->resource('tribut', 'Admin\TributController');
$router->get('tribut.updateStatus', 'Admin\TributController#updateStatus');
});
// Does not work when I call: /tribut/updateStatus URL
$router->group(['prefix' => 'admin', 'before' => 'isLogged|isAdmin'], function ($router) {
$router->resource('tribut', 'Admin\TributController');
$router->get('tribut/updateStatus', 'Admin\TributController#updateStatus');
});
I want to use the second route option. Is it possible? What am I doing wrong? When I call the routing that does not work, the screen goes blank. No error is shown, neither in log files.
The problem is that $router->resource('tribut') registers a route that caches everything with GET tribut/* because it thinks * is an id.
The solution is pretty simple, just define the explicit get route before the resource route:
$router->group(['prefix' => 'admin', 'before' => 'isLogged|isAdmin'], function ($router) {
$router->get('tribut/updateStatus', 'Admin\TributController#updateStatus');
$router->resource('tribut', 'Admin\TributController');
});

cake php calling function of controller and getting controller could not be found

I am learning cake php . I made plugin with folder name Map , every thing goes fine . but when i call other function then index() in MapController class . it gives error
Map.functionName could not be found.
but i made that function in MapController class .
url -- http://localhost/rootfolder/map/functionName
Please tell whats the problem. Can we not able to make other function than index() in plugin ?
Thanks in advance.
For plugins, the default URL path is /:plugin/:controller/:action, so you'll probably need to access /map/map/action. This can be re-written with routes like so:
Router::connect('/something/', array('plugin' => 'map', 'controller' => 'map', 'action' => 'someACtion'));

Resources