Resource routes

Learn how to define resource routes, typically used for create, read, update, and delete (CRUD) operations in a simple and standardized approach.

The method names represent the HTTP methods and paths required by the routes, for example:

Method Route method Route path Equivalent
index GET / $app->action('GET', '/')
create GET /create $app->action('GET', '/')
store POST / $app->action('POST', '/')
edit GET /<id>/edit $app->action('GET', '/<id>/edit')
show GET /<id> $app->action('GET', '/<id>')
update PUT /<id> $app->action('PUT', '/<id>')
destroy DELETE /<id> $app->action('DELETE', '/<id>')

Create controller

To test, create the file system/Controllers/ResourceSample.php and then add the following content to it:

<?php namespace Controller; class ResourceSample extends \Inphinit\Routing\Resource { public function index() { return 'index'; } public function create() { return 'create'; } public function store() { return 'store'; } public function show($app, $params) { $id = $params['id']; ... return 'show'; } public function edit($app, $params) { $id = $params['id']; ... return 'edit'; } public function update($app, $params) { $id = $params['id']; ... return 'update'; } public function destroy($app, $params) { $id = $params['id']; ... return 'destroy'; } }

Then add this to main.php or dev.php:

\Controller\ResourceSample::action($app);

Is equivant to:

$app->action('GET', '/', 'ResourceSample::index'); $app->action('GET', '/create', 'ResourceSample::create'); $app->action('POST', '/', 'ResourceSample::store'); $app->action('GET', '/<id>/edit', 'ResourceSample::edit'); $app->action('GET', '/<id>', 'ResourceSample::show'); $app->action('PUT', '/<id>', 'ResourceSample::update'); $app->action('DELETE', '/<id>', 'ResourceSample::destroy');
Go to homepage
Star us on Github