cakephp-cors icon indicating copy to clipboard operation
cakephp-cors copied to clipboard

cakephp3 Cannot locate dispatcher filter named Cors.Cors

Open jkimilu opened this issue 8 years ago • 2 comments

I am a newbie in cakephp and so far am loving it. Am trying to build a mobile app that will consume json apis within my cake, but i have not been successful as my cross domain requests are being blocked and thats how i have ended up here.

I have the pasted the following in bootstrap.php DispatcherFactory::add('Cors.Cors', ['routes' => [ 'mobilePosController' ]]);

also pasted the cors filter plugin into Plugins folder and renamed the folder from cakephp-cors to just Cors.

Why am getting the error in subject line above.

jkimilu avatar May 16 '16 19:05 jkimilu

figured that i needed to load the plugin and then paste the dispatch code right after. No errors but still getting XMLHttpRequest cannot load http://192.168.0.12/rms/mobile-pos/login.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. Kindly assist how to get ride of this cross domain issue.

jkimilu avatar May 16 '16 19:05 jkimilu

Cake Controller classes follow the UpperCamelCase convention and typically you can omit the 'Controller' from controller class names when they're being called in a CakePHP controller specific context.

  1. Rename your controller to MobilePosController where it's defined.
  2. Redefine your route structure for the Cors dispatch factor config by adjusting for the UpperCamelCase convention and omitting the "Controller" prefix on the class name:
DispatcherFactory::add('Cors.Cors', ['routes' => [
    'MobilePos'
]]);

If this doesn't work for you, I would suggest running the tests and seeing if any tests are failing on your specific version of Cake.

LeWestopher avatar May 16 '16 20:05 LeWestopher