Router failed CakePHP 3.x - cakephp

http://localhost/index
but connect to http://localhost/abc...xyz => THIS WILL BE ERROR PAGE SYSTEM
after, i want connect to http://localhost/index

Unable to understand . Please describe properly.
You may update Config/routes.php
Router::scope('/', function (RouteBuilder $routes) {
/**
* Here, we are connecting '/' (base path) to a controller called 'Pages',
* its action called 'display', and we pass a param to select the view file
* to use (in this case, src/Template/Pages/index.ctp)...
*/
$routes->connect('/index', ['controller' => 'Pages', 'action' => 'index']);
}

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 : link to any action in PagesController opens same action

I have to link to different actions in PagesController.
I have created many static pages and for that I have defined an action like
public function contact(){
}
now when I access www.mysite.com/pages/contact instead of opening contact.ctp it opens the default display action.
The routes.php file contains
Router::defaultRouteClass('DashedRoute');
Router::scope('/', function (RouteBuilder $routes) {
$routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
$routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
$routes->fallbacks('DashedRoute');
});
Plugin::routes();
How can I access the static pages or other actions of PagesController?
$routes->connect('/pages/:action/*', ['controller' => 'Pages']);
Now you can call to diferent actions. e.g.
www.mysite.com/pages/contact
www.mysite.com/pages/about
www.mysite.com/pages/someaction
The default routing for the PagesController is to direct everything to the display action.
In order to add additional actions, you would need to route these specifically.
$routes->connect('/pages/contact', ['controller' => 'Pages', 'action' => 'contact']);
Or, alternatively, if you do not want everything to go through the display action, remove the specific line in routes.php that directs everything there. CakePHP would auto-route anything beginning with /pages/ to the PagesController, and anything after the slash to it's appropriate action.
If you make the view file src/Template/Pages/contact.ctp you can access it using the URL http://example.com/pages/contact
No need to change anythin in routes.php they are fine. No need to create a method in your PagesController, this is a simple and optional controller for serving up static content.

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.

Adding a controller function for the "home" view in CakePHP

When visiting a default CakePHP site, it takes you to "home.ctp" page.
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
I want to add a few elements there (like blog posts), so I thought I could just add this to the PagesController() class:
public function home() {
$this->set('blogposts', $this->Blogpost->find('all'));
}
But that does not work.
So: what is the correct way to add something like this on the home page (or any other page for that matter)
The preferred option is to create a custom route for the home page but you can also override the PagesController's display function
Option 1: (Preferred Method)
Router::connect('/', array('controller' => 'mycontroller', 'action' => 'myaction'));
Option 2
Router::connect('/', array('controller' => 'pages', 'action' => 'home'));
Option 3:
class PagesController {
function display()
{
// default controller code here
// your custom code here
}
}
The final option is using requestAction in your view, but it isn't recommended as it has a huge performance drawback
Option 4: (Not recommended)
$newsitems = $this->requestAction(array('controller' => 'newsitems', 'action' => 'getlatestnews', 'limit' => 10));
In fact, the action is display, home is a parameter. So your main method in the Controller Pages must call display, not home. After that, create display.ctp view.
Reference:
Routing
To actually answer the original question:
$this->loadModel('Blogpost');
Ideally the model should be called
$this->loadModel('BlogPost');

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