I set up the following routes. The routes are explicit and the controller is in src/Controller directory. I did a git pull on the stage server and suddenly cakePHP (3.6) could not find the method, and was looking in the wrong controller. Below are the routes that are explicitly coded.
Router::scope('/', function (RouteBuilder $routes) {
$routes->connect('/<foo>/methodOne/*', ['controller' => 'SomeController', 'action' => 'methodOne']);
$routes->connect('/<foo>/methodTwo/*', ['controller' => 'SomeController', 'action' => 'methodTwo']);
$routes->fallbacks('DashedRoute');
}
This happens with new controllers I create sometimes. I have added the correct namespaces and use statements. To fix this issue I make changes in the controller and then it works. Any idea why this happens? Anyway to prevent this from happening over and over again?
Related
I am setting up my cakephp project on a local test environment from my GIT repo.
it is accessed like so:
localhost/projectName/controller/action
However, i get an error saying the controller "projectName" cannot be found.
So, i need it to use localhost/projectName as my "root" directory, and i am kinda lost as to how. I can obviously define a manual route like:
$routes->connect('/projectName/controller/action', ['controller' => 'Pages', 'action' => 'home']);
but i don't know how i can make it a "catch all" instead of just routing to one specific controller / action?
I hope my question makes sense.
thanks
Iy you have created a localhost with xamp or wamp (and create a folder in www with the name of your project) you just need to write :
$routes->setRouteClass(DashedRoute::class);
$routes->scope('/', function (RouteBuilder $builder) {
$builder->connect('/', ['controller' => 'Pages', 'action' => 'home']);
$builder->connect('/{action}', ['controller' => 'Pages', 'action' => '{action}']);
Hope i understand your question and i can help you !
AO
I have a plugin named as PanelAdmin. It has Controller UsersController.php and inside it there are different actions defined.
I have called the default controller within the plugin through this code
$routes->connect('/PanelAdmin', ['plugin' => 'PanelAdmin','controller' => 'default','action' => 'index']);
but cannot call other controller if i hit this url:
http://localhost/multi_shopping/PanelAdmin/Users/
One thing more i want to clear is i have to define routes for all controllers actions in routes.php. Please solve my issue. Thanks
In your plugin routes.php make sure you are setting a fallback route.
routes.php
<?php
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;
Router::plugin(
'PanelAdmin',
['path' => '/PanelAdmin'],
function (RouteBuilder $routes) {
$routes->fallbacks(DashedRoute::class);
}
);
From the DashedRoute class:
/**
* This route class will transparently inflect the controller, action and plugin
* routing parameters, so that requesting `/my-plugin/my-controller/my-action`
* is parsed as `['plugin' => 'MyPlugin', 'controller' => 'MyController', 'action' => 'myAction']`
*/
I try to use a router with phalcon. This is how it is included in index.php right after registering the 'events manager':
$di->set('router', function(){
require __DIR__.'/../app/config/routes.php';
return $router;
});
and this is how the routes.php looks like:
<?php
$router = new Phalcon\Mvc\Router(false);
$router->add("/", array(
'controller' => 'index',
'action' => 'index'
));
$router->add("/topics", array(
'controller' => 'wurst',
'action' => 'index'
));
$router->handle();
return $router;
The website reacts as if the router was not existent. /topics and topics say this:
TopicsController handler class cannot be loaded
and I also cannot use a die("test"); function inside routes.php . nothing happens.
I also tried to activate it without a separate file, but the result was the same :(
(The sample web-application INVO was used as starting point for my site )
$router->setUriSource(Router::URI_SOURCE_SERVER_REQUEST_URI); will use default $_SERVER['REQUEST_URI']
If your index/index action is working when you access domain.com/index.php, check that you are using proper uri source, if using nginx or php built-in server you might have some problems with routing and $_GET['_uri'] which phalcon use for handling uris.
can find more about it on phalcon router documentation about uri sources -> http://docs.phalconphp.com/en/latest/reference/routing.html#uri-sources
now it seems to work:
Action 'route404' was not found on handler 'index'
the problem was , that I put the function to set the router in index.php within "set dispatcher function". ..did not see the closing braces.
So I want to add backward in-compatible changes to my API.
I was thinking of doing the following.
All of my api endpoints are accessed as follows:
/v2/account
/v2/order
Having a v2 controller that is passed an API version and calls the appropriate function in version specific controllers that are subclass to v2.
so for version 2013_02_13 it calls v2_2013_02_13::account for account api call
How would you implement an API versioning system to support backward incompatible changes using a PHP mvc framework?
In Kohana you could just use a directory for that. So your controllers would be placed like this.
application/classes/Controllers/V2_2013_02_13/Enpoint1Controller
Than you can set up routes for your different versions.
Route::set('v2', 'v2/<controller>(/<action>)')
->defaults(array(
'directory' => 'V2_2013_02_13',
'controller' => 'welcome',
'action' => 'index',
));
This would be the most easy approach, however if you really want to have some dynamic way to call specific versions of your controllers than I would look at the HMVC of Kohana.
My guess would be, that you need an entry controller for each version you have and do an internal request to the correct controller.
Maybe something like this.
Route:
Route::set('versioned', '<version>/<someAction>')
->defaults(array(
'version' => 'v2',
'someAction' => 'user'
'controller' => 'welcome',
'action' => 'index',
));
Controller:
class Controller_Welcome extends Controller {
public function action_index()
{
// Your Version and the action
$this->request->param('version');
$this->request->param('someAction');
// Do an internal request to the right controller (the v2/user is an example)
$internalRequest = Request::factory('v2/user');
}
}
I hope this helps.
I recently started working with the kohana 3.3.1 framework and ran into some problems.
I want to create different routes for different entry points. Right now, this is the default route, which seems to work fine(I think):
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'welcome',
'action' => 'index',
));
When I go to the website, it displays 'hello, world!'.
I have a controller called Street, located in application/classes/Controller/Street.php.
The code to this controller is:
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Street extends Controller {
public function action_index()
{
$this->response->body('hello, street!');
}
The problem is, that I want to type /something behind the URI, and it should redirect to the defined controller, and action. But it doesn't seem to work. I get 404 error's when I type ANYTHING behind the default URI.
For routes, i use this
/**
* Set the routes. Each route must have a minimum of a name, a URI and a set of
* defaults for the URI.
*/
Route::set('test1', 'street/<id>')
->defaults(array(
'controller' => 'street',
'action' => 'index',
));
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'welcome',
'action' => 'index',
));
Any help would be gladly appreciated.
EDIT
I just tried #Darsstar 's instruction to go to /index.php/street, and it worked!
But now, when I try to go to /index.php/street/derp, I get an error saying
The requested URL derp was not found on this server.
So it's not yet working properly I guess
Since the /index.php/street version works go and read the Clean URLs tutorial if you haven't already. If you have, double check everything!
If you have just those two routes, in that order, /index.php/street/derp should have matched the route 'test1'.
The error message 'The requested URL derp was not found on this server.' says you went to /index.php/derp, not index.php/street/derp. Which would match the default route and be dispatched to Controller_Derp::action_index(), but it doesn't exist so Request_Internal::execute() throws a HTTP_Exception_404.
The default route is more of and example. The is a discussion on Kohana's issue tracker to remove it since a catchall default route is a bad practice. Routes should be specific. I recommend to remove it.
And if you think having a catchall route is a good way to catch all 404 requests, please let the Custom Error Pages tutorial prove you wrong.