Skip to content

Instantly share code, notes, and snippets.

@markstory
Created November 22, 2014 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markstory/76a8c2e3af3da787a6b5 to your computer and use it in GitHub Desktop.
Save markstory/76a8c2e3af3da787a6b5 to your computer and use it in GitHub Desktop.
<?php
Router::scope('/', function ($routes) {
$routes->extensions('json');
/**
* 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/home.ctp)...
*/
$routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
/**
* ...and connect the rest of 'Pages' controller's URLs.
*/
$routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
/**
* Connect a route for the index action of any controller.
* And a more general catch all route for any action.
*
* The `fallbacks` method is a shortcut for
* `$routes->connect('/:controller', ['action' => 'index'], ['routeClass' => 'InflectedRoute']);`
* `$routes->connect('/:controller/:action/*', [], ['routeClass' => 'InflectedRoute']);`
*
* You can remove these routes once you've connected the
* routes you want in your application.
*/
$routes->fallbacks();
});
curl -XPATCH -H 'Content-Type: application/json' -H 'Accept: application/json' cake3.localhost/tags/edit/2 -d '{"title": "some new title"}'
<?php
namespace App\Controller;
use App\Controller\AppController;
/**
* Tags Controller
*
* @property App\Model\Table\TagsTable $Tags
*/
class TagsController extends AppController {
public $components = ['Flash', 'RequestHandler'];
public function edit($id = null) {
$tag = $this->Tags->get($id, [
'contain' => ['Bookmarks']
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$tag = $this->Tags->patchEntity($tag, $this->request->data);
if ($this->Tags->save($tag)) {
$this->Flash->success('The tag has been saved.');
// return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error('The tag could not be saved. Please, try again.');
}
}
$bookmarks = $this->Tags->Bookmarks->find('list');
$this->set(compact('tag', 'bookmarks'));
$this->set('_serialize', ['tag']);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment