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.
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>
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']);
I want to remove deal/deals for all the actions of deals controller:
http://example.com/deal/deals/voucher/dsfsdfdf should be http://example.com/voucher/cPH5aGr1
etc.
You should define you routes in you app/config/routes.php
Router::connect('/voucher/*', array('controller' => 'Deals', 'action' => 'voucher', 'plugin' => 'Deal'));
I'm creating a website using CakePHP.
It has user Registration and Login system.
So at first i have a controller called HomeController which has three methods
1) index();
2) login();
3) register();
by default index() method will execute.
i have the following code to call other two methods.
<? php
echo $this->Html->link('Login',array('controller'=>'Home','action'=>'login'),array('escape'=>FALSE));
echo $this->Html->link('Register',array('controller'=>'Home','action'=>'register'),array('escape'=>FALSE));
?>
So now when i click on the above links (login,register) it will call appropriate method and the url will be something like
www.example.com/home/login and www.example.com/home/register
Now i want to remove the controller name from the url since the method is in the same controller.
So the url should look like
www.example.com/login and www.example.com/register
is it possible??
Please help..
in app\Config\routes.php add following lines
Router::connect('/login', array('controller' => 'home', 'action' => 'login'));
Router::connect('/register', array('controller' => 'home', 'action' => 'register'));
It will convert default urls to your desired urls
www.example.com/login and www.example.com/register
Router::parseExtensions('json');
Router::connect('/*', array('controller' => 'home',
'action' => 'login'));
Router::connect('/login', array('controller' => 'home',
'action' => 'login'));
TLDR: How can I create a URL in the Controller similar to how I can use the HtmlHelper to create URLs in a View?
Problem:
I want to print the url of a controller action, in my controller (because I create my JSON string in my controller, not in a view)
In a View, I can use $this->Html->url(), but what about in a Controller?
Should I use defined constant like APP_DIR + Controller name + Controller action?)
Use the Router class.
$url = Router::url([
'controller' => 'Articles',
'action' => 'index',
'?' => ['page' => 1],
'#' => 'top'
]);
or the same thing, but in a more common/simple scenario:
$url = Router::url(['controller' => 'Articles', 'action' => 'index']);
Note: in Cake2.x, "Articles" would be lowercase.
CakePHP 2.x Router documentation
CakePHP 3.x 'Generating URLs' documentation