Why iam getting a "Missing Route" error for a function that not exists.
Inside Reservations Controller I have function add with one argument:
public function add($carid = null)
{
...
}
Matching route:
Router::scope('/', function ($routes) {
Router::connect('/rentcar/:id', ['controller' => 'Reservations', 'action' => 'add'],['pass' => ['id'], 'id' => '[0-9]+']);
// rest of the routes not important
...
});
Plugin::routes();
When I visit any page I see the following error:
http://i.stack.imgur.com/ESX5I.jpg
The error message says:
Error: A route matching "array ( 'controller' => 'Reservations',
'action' => 'add', 'plugin' => NULL, '_ext' => NULL, )" could not be
found.
...which is strange because I dont have function add() without arguments, instead, I have function add($carid) with one argument.
But when i add that route, everything works fine:
Router::connect('/rentcar2', ['controller' => 'Reservations', 'action' => 'add']);
What is going on?
The problem was in commented out HTML code which contains PHP code inside, code like this one:
<!-- <li role="presentation"><?= $this->Html->link(__('New Reservation'), ['controller' => 'Reservations', 'action' => 'add']) ?></li> -->
Which contains my old function without arguments add of Reservations Controller.
I deleted any similar code and everything worked fine.
Related
I know POST method is pointing to add() in a controller as default in cakephp3. Is it possible to custom that and point POST method to index()? Something like below:
Router::connect(
'/test',
array(
'controller' => 'Test',
'action' => 'index',
'[method]' => 'POST'
)
);
Thanks to #ndm who made a very clear solution for my question.
One of my issue is that I have $routes->resources('Test'); which will disable #ndm's solutions. So first of all, I commented out the line $routes->resources('Test');.
Because of I'm not working on a solid project which is a temporary project for a narrowed purpose, so below code is working perfectly for me now.
Router::scope('/', function ($routes) {
$routes->setExtensions(['json']);
// $routes->resources('Test');
$routes->post(
'/test',
['controller' => 'Test', 'action' => 'add']
);
});
I'm trying to load the VideoController.php when url.com/video/ is called.
Here is a piece of code from my routes.php
Router::scope('/', function (RouteBuilder $routes) {
$routes->connect('/video/*', ['controller' => 'Video', 'action' => 'display']);
...
$routes->fallbacks(DashedRoute::class);
});
The result is '404 not found'
EDIT:
Inside Router::scope I have also this piece of code:
$routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
When I change the Pages value into Video, the VideoController is called. So how come when I change the '/' into '/video/*' it doesn't work?
Maybe it's something with the scope function parameters?
Enabling URL rewriting from the httpd.config file solved the problem.
cakephp documentation
When i enter URL http://localhost/cake2/cruds with GET VERB
method View() loads. i want to load index() method... when i enter URL http://localhost/cake2/cruds/8 with GET VERB same happens.
Remaining Routes works fine.
My Routes are:
Router::connect('/', array('[method]'=>'GET','controller' => 'Cruds', 'action' => 'index'));
Router::connect('/', array('[method]'=>'POST','controller' => 'Cruds', 'action' => 'add'));
Router::connect('/:id', array('[method]'=>'GET','controller' => 'Cruds', 'action' => 'view','id'));
Router::connect('/:id', array('[method]'=>'PUT','controller' => 'Cruds', 'action' => 'edit','id'));
Router::connect('/:id', array('[method]'=>'DELETE','controller' => 'Cruds', 'action' => 'delete','id'));
Same Routes works fine in CakePHP v3.
My Controller Methods are:
function index()
{
$this->loadModel("crud");
$users = $this->crud->find('all');
//var_dump($users);
$users = Set::extract($users, '{n}.crud');
$this->set('message', json_encode($users,JSON_PRETTY_PRINT));
}`
`function view($id)
{
$this->loadModel("crud");
$user = $this->crud->findById($id);
if (!$user) {
$this->set('message', "User Not Found..!");
}
else
{
$this->set('message',json_encode($user['crud'],JSON_PRETTY_PRINT));
}
}
this line clearly says that it will call view action on id
Router::connect('/:id', array('[method]'=>'GET','controller' => 'Cruds', 'action' => 'view','id'));
so as you want to call it index so we added index instead of view
Router::connect('/:id', array('[method]'=>'GET','controller' => 'Cruds', 'action' => 'index','id'));
(& i dont know cake php)
Router::scope('/:club_slug', function ($routes) {
$routes->connect('/login', ['controller' => 'Users', 'action' => 'login']);
});
So when I'm trying access http://example.com/club-name/login, I'm being redirected to http://example.com/users/login with the flash message You have to login to access this area.
Auth loginAction is [controller => 'Users', 'action' => 'login'], since the custom route that I mentioned at beginning of the question is pointing to the path that is specified at loginAction I thought the route will know that I'm talking about the same thing but is not what is happening.
Dynamic route elements are not being added/recognized automatically, you'll either have to persist them using either the persist option (applies only to that specific route):
Router::scope('/:club_slug', function ($routes) {
$routes->connect(
'/login',
['controller' => 'Users', 'action' => 'login'],
['persist' => ['club_slug']]
);
});
or URL filters (affects all routes that are using a club_slug element):
Router::addUrlFilter(function ($params, $request) {
if (isset($request->params['club_slug']) && !isset($params['club_slug'])) {
$params['club_slug'] = $request->params['club_slug'];
}
return $params;
});
or you have to pass the element to your login action manually (this would match the club_slug route regardless of the current URL):
'loginAction' => [
'controller' => 'Users',
'action' => 'login',
'club_slug' => 'club-slug' // wherever it may have come from
]
See also
Cookbook > Routing > Creating Persistent URL Parameters
API > Cake\Routing\RouteBuilder::connect()
CakePHP 3.0
I'm getting a "Missing Route" error for a route that exists.
Here are my routes:
#my admin routes...
Router::prefix('admin', function($routes) {
$routes->connect('/', ['controller'=>'Screens', 'action'=>'index']);
$routes->connect('/screens', ['controller'=>'Screens', 'action'=>'index']);
$routes->connect('/screens/index', ['controller'=>'Screens', 'action'=>'index']);
//$routes->fallbacks('InflectedRoute');
});
Router::scope('/', function ($routes) {
$routes->connect('/login', ['controller' => 'Pages', 'action' => 'display', 'login']);
$routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
$routes->fallbacks('InflectedRoute');
});
Plugin::routes();
Basically I just added the top section (for admin routing) to the default routes that come out of the box.
When I visit /admin/screens/index I see the following error:
Notice the error message says:
Error: A route matching "array ( 'action' => 'add', 'prefix' =>
'admin', 'plugin' => NULL, 'controller' => 'Screens', '_ext' => NULL,
)" could not be found.
...which is strange because I am not trying to access the add action. The params printed below look correct.
What is going on?
Take a closer look at the stacktrace, the error dosn't occour in the dispatching process, which you seem to think, it is being triggered in your view template, where you are probably trying to create a link to the add action, and reverse-routing cannot find a matching route, hence the error.
The solution should be obvious, connect the necessary routes, being it explicit ones like
$routes->connect('/screens/add', ['controller' => 'Screens', 'action' => 'add']);
catch-all ones
$routes->connect('/screens/:action', ['controller' => 'Screens']);
or simply the fallback ones that catch everything
$routes->fallbacks('InflectedRoute');
This work for me in case of use prefix admin :-
Router::prefix('admin', function ($routes) {
// Because you are in the admin scope,
// you do not need to include the /admin prefix
// or the admin route element.
$routes->connect('/', ['controller' => 'Users', 'action' => 'index']);
$routes->extensions(['json', 'xml']);
// All routes here will be prefixed with `/admin`
$routes->connect('/admin', ['controller' => 'Order', 'action' => 'index']);
// And have the prefix => admin route element added.
$routes->fallbacks(DashedRoute::class);
});