angularjs - How to connect a post rest request to the add controller action using prefixes in cakephp -



angularjs - How to connect a post rest request to the add controller action using prefixes in cakephp -

i code client/server application.

server side powered cakephp 2.4.7. client side run angularjs , cordova on mobile devices

i utilize several cakephp route prefixes 'admin' , 'mobile'. (and utilize $resource , $httpinterceptor angularjs factories setup rest requests.)

i want phone call /website/mobile/posts/add.json json info posting.

so phone call /website/mobile/posts.json post ajax query:

the problem here: called controller action 'index', not 'add';

on top of postscontroller, added this:

echo $this->request->params['action'] . "\n"; echo ($this->request->ispost())? "post" . "\n" : "not post"; die;

and response always:

mobile_index post

so ajax request seems right cakephp don't map add together action, index one. configuration seems too; here fragment of routes.php

router::mapresources(array('users','posts')); router::parseextensions('json');

any thought ?

prefix routing doesn't work rest routing out of box

rest routing works automatically creating routes controllers passed router::mapresources(). prefix routing pretty much same, creates default routes including prefixes defined in routing.prefixes.

however both functionalities don't know each other, both create separte routes, router::mapresources() connect urls without prefixes (the prefix alternative method not using actual prefix routing, add together value of alternative origin of url connect to!), , hence request /mobile/... doesn't utilize rest routing prefix routing.

defining prefixed rest routes manually

there no simple prepare problem using alternative or something, instead you'll have define rest routes manually prefix alternative included, simple plenty though.

see modifying default rest routes , custom rest routing.

a sample route this:

router::connect( '/mobile/users', array( 'prefix' => 'mobile', 'mobile' => true, 'controller' => 'users', 'action' => 'add', '[method]' => 'post' ) );

this connect post requests /mobile/users userscontroller::mobile_add(). similary you'll have other methods get , put, , without passing id, etc.

note when connecting manually can ditch routing.prefixes alternative , of course of study phone call router::mapresources().

automated mapping

defining routes hand kinda exhausting, you're improve of automating respect router::resourcemap() configuration.

here's illustration on how that, it's similar router::mapresources(), accepts prefix alternative makes utilize of prefix routing:

function mapresources(array $controllers) { $resourcemap = router::resourcemap(); foreach($controllers $controller => $options) { if(!is_array($options)) { $controller = $options; $options = array(); } $options += array( 'prefix' => null, 'plugin' => null, 'id' => router::id . '|' . router::uuid ); foreach($resourcemap $params) { $url = ''; if($options['prefix']) { $url .= '/' . $options['prefix']; } if($options['plugin']) { $url .= '/' . $options['plugin']; } $url .= '/' . $controller; if($params['id']) { $url .= '/:id'; } router::connect( $url, array( 'prefix' => $options['prefix'], $options['prefix'] => !!$options['prefix'], 'plugin' => $options['plugin'], 'controller' => $controller, 'action' => $params['action'], '[method]' => $params['method'] ), array( 'id' => $options['id'], 'pass' => array('id') ) ); } } }

you phone call this:

mapresources(array( 'books' => array( 'prefix' => 'mobile' ) ));

and map rest routes books controller using mobile prefix.

angularjs rest cakephp cakephp-2.0 ionic-framework

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

c# - Create a Notification Object (Email or Page) At Run Time -- Dependency Injection or Factory -

Set Up Of Common Name Of SSL Certificate To Protect Plesk Panel -