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']);
Related
I am new on Cakephp. I am using latest version of cakephp. I have created an controller "PostsController" and want to make it home page. But when I have set it to home page from routes.php nothing happens. I am using subdomain like - cakephp.example.com. Here is my routes.php code
$routes->connect('/', ['controller' => 'Posts', 'action' => 'index', 'home']);
Can anyone please help me why it is not working? Is there anything need to do in htaccess file?
Router: (only once in your router class)
$routes->connect('/', ['controller' => 'Posts', 'action' => 'index']);
src/Controllers/PostsController:
public function index()
{
// your code here
}
src/Template/Posts/index.ctp
<h1>Hello world</h1>
I am creating a plugin that I want integrated within the admin section of my application. My application structure for the admin section looks like this:
src/Controller/Admin/AdminsController.php
src/Controller/Admin/ProductsController.php
src/Controller/Admin/BlogsController.php
AdminsController.php looks like this:
namespace App\Controller\Admin;
use App\Controller\AppController;
use Cake\Event\Event;
use Cake\Network\Exception\ForbiddenException;
class AdminsController extends AppController{
And my Admin controllers, ie BlogsController.php looks like this:
namespace App\Controller\Admin;
use App\Controller\Admin\AdminsController;
class BlogsController extends AdminsController {
My plugin has a FeedbacksController that looks just like the Blogs Controller above, which also uses AdminsController from the application: plugin/AkkaFeedback/src/Controller/Admin/FeedbacksController.php
namespace App\Controller\Admin;
use App\Controller\Admin\AdminsController;
class FeedbacksController extends AdminsController {`
Also, within my plugin I have plugin/AkkaFeedback/src/Controller/FeedbacksController.php
My Intention is to have /admin/feedbacks point to this controller. Is this even possible within CakePHP? I have tried many possibilities without success. Here is what I have tried as well as others without success:
Router::prefix('admin', function ($routes) {
$routes->connect('/', ['controller' => 'Dashboards', 'action' => 'index']);
$routes->connect('/feedbacks', ['plugin' => 'AkkaFeedback', 'controller' => 'Feedbacks', 'action' => 'index']);
// I have tried this
//$routes->connect('/feedbacks', ['plugin' => 'AkkaFeedback', 'controller' => 'Feedbacks', 'action' => 'index', 'prefix' => 'admin']); // I have also tried this
// And this without succcess
// /admin/akka_feedback/feedbacks
// $routes->plugin('AkkaFeedback', function ($routes) {
// $routes->connect('/:controller');
// });
$routes->fallbacks('InflectedRoute');
});
The error I get is: Controller class Feedbacks could not be found., but there is a Feedbacks class, both in Controller and Controller/Admin within the plugin.
Not sure what else to try. Any ideas would be appreciated!
After some more researching I was able to make it work by adding the following to the plugin's routes.php file plugins/AkkaFeedback/config/routes.php:
Router::prefix('admin', function ($routes) {
$routes->plugin('AkkaFeedback', function ($routes) {
$routes->connect('/:controller', ['action' => 'index'], ['routeClass' => 'DashedRoute']);
$routes->connect('/:controller/:action/*', [], ['routeClass' => 'DashedRoute']);
});
});
In conjunction with the routes added to the routes.php file of the application.config/routes.php
Router::prefix('admin', function ($routes) {
$routes->connect('/', ['controller' => 'Dashboards', 'action' => 'index']);
$routes->connect('/feedbacks', ['plugin' => 'AkkaFeedback', 'controller' => 'Feedbacks', 'action' => 'index']);
$routes->connect('/feedbacks/:action/*', ['plugin' => 'AkkaFeedback', 'controller' => 'Feedbacks']);
$routes->fallbacks('InflectedRoute');
});
I am not sure if this is the best way, but it works for now.
Your FeebacksController namespace declaration isn't correct. It should be:
namespace AkkaFeedback\Controller\Admin;
use App\Controller\Admin\AdminsController;
class FeebacksController extends AdminsController {`
In config/routes.php:
Router::prefix('admin', function ($routes) {
$routes->fallbacks(DashedRoute::class);
});
In plugins/CustomPlugin/config/routes.php:
Router::prefix('admin', function ($routes) {
$routes->plugin('CustomPlugin', ['path' => '/custom-plugin'], function ($routes) {
$routes->fallbacks(DashedRoute::class);
});
});
It seems the best method and it works properly.
In this way i can have an Admin section in the root project and also in the plugin.
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.
How do I show the URL through CroogoRouter::connect function in CakePHP?
I passed the name of controller and action my routes file like:
CroogoRouter::connect('/', array('admin' => true, 'controller' => 'dashboard', 'action' => 'index'));
and I get redirected to the dashboard page, but I do not see the URL properly. I want my URL as localhost/abc/admin/dashboard and it is showing as localhost/abc only.
Since you are using the webroot (/) as route alias, it will not show the admin/dashboard bit. If you really want that (I'd discourage it if it's just for fancy display purposes), you should create a simple controller action that redirects. For example if you alter the route to this:
CroogoRouter::connect('/', array('controller' => 'dashboard', 'action' => 'home'));
And then in the DashboardsController create this action:
public function home() {
$this->autoRender = false; // We have no view, so don't render anything
$this->redirect(array('admin' => true, 'controller' => 'dashboard', 'action' => 'index'));
}
It should display the URL as you want it. Again, if it's only for display purposes (to make it look "nice") and not for any SEO kind of purpose, I'd discourage using such an ugly workaround. But it should do the trick either way.
Can you please check the controller name i think controller name should be dashboards or something.
check this link may be helpful http://bakery.cakephp.org/articles/Frank/2009/11/02/cakephp-s-routing-explained
I'm developing a simple CMS in CakePHP, right now it has 4 controllers in it(Menus,Site,Roles,Users), I want to rewrite one controller, but I'm having problem.
I use all the actions only as admins for admin purpose like admin_view, admin_add......
except siteController(this controller is only for frontend purpose)
I need my www.example.com/site/view/something_here must be replaced to www.example.com/something_here - this will be displayed in front-end so.
I added a line in my routes file:
Router::connect('/*', array('controller' => 'site', 'action' => 'view'));
But after adding this I couldn't able to use other controllers.
I again added some more lines before the above line:
Router::connect('/admin/Menus/*', array('controller' => 'menus', 'prefix' => 'admin'));
Same for all other controllers, but if I send any action or id in url it doesn't works.
like - http://www.exmple.com/admin/menus/[view/1] - the one inside square bracket doesn't works.
any Ideas on rewriting this?
I just answered a similar question on another thread.
To put the admin controller routes before the '/*'-route was the right idea, but the way you did it the router can't assign an action. You could use the following for each controller:
Router::connect('/admin/Menus/:action/*', array('controller' => 'menus', 'prefix' => 'admin'));
Or you could use the default prefix-routing routes, so you don't have to add a route for each new controller.
// prefix routing default routes with admin prefix
Router::connect("/admin/:controller", array('action' => 'index', 'prefix' => 'admin', 'admin' => true));
Router::connect("/admin/:controller/:action/*", array('prefix' => 'admin', 'admin' => true));