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

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

Related

CakePHP 3 use default routing in some cases

I have a CakePHP 3.5 website. It was necessary to set slugs without any url prefixes like /pages/slug, so I wrote the following rule:
$routes->connect(
'/:slug',
['controller' => 'Pages', 'action' => 'redirectPage']
)
->setMethods(['GET', 'POST'])
->setPass(['slug'])
->setPatterns([
'slug' => '[a-z0-9\-_]+'
]);
It works nice, but in some cases I want cakePHP to route as default (Controller/Action/Params). For example, I want /admin/login to call 'login' action in 'AdminController'.
I have two ideas that doesn't need exact routings, but I can't make any of them to work:
Filtering some strings by pattern: It would be nice if I could filter some strings, that if slug doesn't match pattern, it will simply skip the routing rule.
Create a '/admin/:action' routing rule, but then I cant use :action as an action variable. It causes errors.
$routes->connect(
'/admin/:action',
['controller' => 'Admin', 'action' => ':action']
)
Any ideas?
Thanks
You can use prefix for admin restricted area.
Example:
Router::prefix('admin', function ($routes) {
$routes->connect('/', ['controller' => 'Users', 'action' => 'login']);
$routes->fallbacks(DashedRoute::class);
});
$routes->connect('/:slug' , [
'controller' => 'Pages',
'action' => 'display',
'plugin' => NULL
], [
'slug' => '[A-Za-z0-9/-]+',
'pass' => ['slug']
]);
Now, for example, path /admin/dashboard/index will execute method in Admin "subnamespace" \App\Controller\Admin\DashboardController::index()
Its nicely described in docs: https://book.cakephp.org/3.0/en/development/routing.html#prefix-routing
Try this:
$routes->connect(
'/admin/:action',
['controller' => 'Admin'],
['action' => '(login|otherAllowedAction|someOtherAllowedAction)']
);
Also, your slug routes seems to not catch /admin/:action routes, b/c dash is not allowed there: [a-z0-9\-_]+

cakePHP 3.0 Action PagesController::myaction.json() could not be found, or is not accessible."

I am migrating an existing 2.5 app over to 3.0. I am getting an 404 error when using ajax. This works fine in cakePHP 2.5
url: "/cakephp3/pages/myaction.json"
I don't see any step that I might have missed.
I am sure it is a routing issue with the .json extension
routes.php
Router::scope('/', function ($routes) {
Router::extensions(['json', 'xml']);
$routes->connect('/', ['controller' => 'Pages', 'action' => 'home']);
$routes->connect('/hotel-training-courses', ['controller' => 'pages', 'action' => 'trainingCourses']);
$routes->connect('/feature-tour', ['controller' => 'pages', 'action' => 'features']);
$routes->connect('/contact-us', ['controller' => 'pages', 'action' => 'contact']);
$routes->fallbacks('InflectedRoute');
});
PagesController.php
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
}
public function myaction(){
$this->request->onlyAllow('ajax');
$userName = $this->request->data['name'];
$userCompany = $this->request->data['company'];
$userEmail = $this->request->data['email'];
$userPhone = $this->request->data['phone'];
//send an email
}
The previous app was able to detect the request type and return with the same type. There was no need to set the render.
Global extensions must be defined outside of scopes
Router::extensions() must be placed outside of, and in case it should apply to all routes, invoked before defining any scopes and routes.
In case you want to restrict extension parsing to a specific scope, use RouteBuilder::extensions(), ie either
Router::extensions(['json', 'xml']);
Router::scope('/', function (RouteBuilder $routes) {
$routes->connect('/', ['controller' => 'Pages', 'action' => 'home']);
//...
});
or
Router::scope('/', function (RouteBuilder $routes) {
$routes->extensions(['json', 'xml']);
$routes->connect('/', ['controller' => 'Pages', 'action' => 'home']);
//...
});
See Cookbook > Routing > Routing File Extensions
Request::onlyAllow() doesn't exist anymore
Request::onlyAllow() has been renamed to Request::allowMethod(), so that's the next problem that you'll encouter.
See
Cookbook > 3.0 Migration Guide > Network > Request
\Cake\Network\Request::allowMethod()
Enable debug mode
Also you should enable debug mode so that you receive meaningful error messages with the appropriate details, necessary to debug such problems.

How to access plugin in 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)

cakephp url rewriting not working properly in the router file

I am facing some problem with rewriting cakephp urls
Here is my url:
domain.com/users/members/mygroup
domain.com/users/admins/mygroup
I want to rewrite this to
domain.com/mygroup/members
domain.com/mygroup/admins
I have tried the following code but it's not working
In the routes.php i have created the following routes
Router::connect('/:groupname/:members', array('controller' => 'users', 'action' => 'members'),array('pass' => array('groupname')));
Router::connect('/:groupname/:admins', array('controller' => 'users', 'action' => 'admins'),array('pass' => array('groupname')));
Here is the links:
<?php echo $this->html->link('Members',array('controller'=>'users','action'=>'members','groupname'=>$groupdata['Group']['group_slug'],'members'=>members),array('escape'=>false,'class'=>'links','id'=>'memlink'));?>
<?php echo $this->html->link('Admin',array('controller'=>'users','action'=>'admins','groupname'=>$groupdata['Group']['group_slug'],'admins'=>admins),array('escape'=>false,'class'=>'links','id'=>'admnlink'));?>
When i create routes like this the first routing i.e member routing is working fine, but the second routing admin is not working, it's picking the members action and executing the members method but the url appears correct, only the action is wrong.
How can i resolve this.
Try this.
Router::connect('/:groupname/:members', array('controller' => 'users', 'action' => 'members'),array('pass' => array('groupname', 'members')));
Router::connect('/:groupname/:admins', array('controller' => 'users', 'action' => 'admins'),array('pass' => array('groupname', 'admins')));
Update
Router::connect('/:groupname/:action', array('controller' => 'users'),array('pass' => array('groupname')));
Router::connect('/:groupname/:action', array('controller' => 'users'),array('pass' => array('groupname')));
working fine for me, put the code like this,
echo $this->html->link('Members',array(
'controller'=>'users','action'=>'members',
'groupname'=> 'mygroup'), array(
'escape'=>false,'class'=>'links','id'=>'memlink'
));
echo $this->html->link('Admin',array(
'controller'=>'users','action'=>'admins',
'groupname'=> 'mygroup'),array(
'escape'=>false,
'class'=>'links','id'=>'admnlink'
));

How can I redirect the default home page to another page in CakePHP?

I need to redirect the default CakePHP home page / or (/pages/home) to /users/dashboard page
I tried
Router::connect('/', array('controller' => 'users', 'action' => 'dashboard'));
and
Router::connect('/pages/home', array('controller' => 'users', 'action' => 'dashboard'));
But both are not working
You should be able to do this by simply replacing this part of app/config/routes.php:
/**
* 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/views/pages/home.ctp)...
*/
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
.. with something like this:
/**
* Here, we are connecting '/' (base path) to controller called 'Users' and
* its action called 'dashboard' (ie. /users/dashboard)...
*/
Router::connect('/', array('controller' => 'users', 'action' => 'dashboard'));
I sense a slight misunderstanding of the topic when you try to map from '/pages/home' to your dashboard. '/pages/home' only seems like the home page because there exists a route for that. If you want to change the homepage, you need to change the existing Router::connect('/', ...) rule. If you create a new rule for '/', underneath, it won't be executed as CakePHP will match the first route it finds.
your first attempt
Router::connect('/', array('controller' => 'users', 'action' => 'dashboard'));
Is the correct way to do it. If you are still having problems then there must be another issue.
What error do you see?

Resources