Skip to content

Commit 3005744

Browse files
committed
Fix RouteCollection::presenter() and test
1 parent 47d955c commit 3005744

4 files changed

Lines changed: 111 additions & 12 deletions

File tree

system/Router/RouteCollection.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
<?php
2-
3-
42
/**
53
* CodeIgniter
64
*
@@ -899,7 +897,7 @@ public function resource(string $name, array $options = null): RouteCollectionIn
899897
* HTTP Verb | Path | Action | Used for...
900898
* ----------+-------------+---------------+-----------------
901899
* GET /photos index showing all array of photo objects
902-
* GET /photos/{id} show showing a specific photo object, all properties
900+
* GET /photos/show/{id} show showing a specific photo object, all properties
903901
* GET /photos/new new showing a form for an empty photo object, with default properties
904902
* POST /photos/create create processing the form for a new photo
905903
* GET /photos/edit/{id} edit show an editing form for a specific photo object, editable properties
@@ -959,31 +957,39 @@ public function presenter(string $name, array $options = null): RouteCollectionI
959957
}
960958
if (in_array('show', $methods))
961959
{
962-
$this->get($name . '/' . $id, $newName . '::show/$1', $options);
960+
$this->get($name . '/show/' . $id, $newName . '::show/$1', $options);
963961
}
964962
if (in_array('new', $methods))
965963
{
966964
$this->get($name . '/new', $newName . '::new', $options);
967965
}
968966
if (in_array('create', $methods))
969967
{
970-
$this->post($name, $newName . '::create', $options);
968+
$this->post($name . '/create', $newName . '::create', $options);
971969
}
972970
if (in_array('edit', $methods))
973971
{
974-
$this->get($name . '/' . $id . '/edit', $newName . '::edit/$1', $options);
972+
$this->get($name . '/edit/' . $id, $newName . '::edit/$1', $options);
975973
}
976974
if (in_array('update', $methods))
977975
{
978-
$this->post($name . '/' . $id, $newName . '::update/$1', $options);
976+
$this->post($name . '/update/' . $id, $newName . '::update/$1', $options);
979977
}
980978
if (in_array('remove', $methods))
981979
{
982-
$this->get($name . '/' . $id, $newName . '::remove/$1', $options);
980+
$this->get($name . '/remove/' . $id, $newName . '::remove/$1', $options);
983981
}
984982
if (in_array('delete', $methods))
985983
{
986-
$this->post($name . '/' . $id, $newName . '::delete/$1', $options);
984+
$this->post($name . '/delete/' . $id, $newName . '::delete/$1', $options);
985+
}
986+
if (in_array('show', $methods))
987+
{
988+
$this->get($name . '/' . $id, $newName . '::show/$1', $options);
989+
}
990+
if (in_array('create', $methods))
991+
{
992+
$this->post($name, $newName . '::create', $options);
987993
}
988994

989995
return $this;

tests/system/RESTful/ResourcePresenterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public function testResourceDelete()
160160
];
161161
$_SERVER['argc'] = 3;
162162
$_SERVER['REQUEST_URI'] = '/work/delete/123';
163-
$_SERVER['REQUEST_METHOD'] = 'GET';
163+
$_SERVER['REQUEST_METHOD'] = 'POST';
164164

165165
ob_start();
166166
$this->codeigniter->useSafeOutput(true)->run($this->routes);

tests/system/Router/RouteCollectionTest.php

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ public function testHostnameOption()
326326

327327
//--------------------------------------------------------------------
328328

329-
public function testResourcesScaffoldsCorrectly()
329+
public function testResourceScaffoldsCorrectly()
330330
{
331331
$routes = $this->getCollector();
332332
$routes->setHTTPVerb('get');
@@ -383,6 +383,97 @@ public function testResourcesScaffoldsCorrectly()
383383
$this->assertEquals($expected, $routes->getRoutes());
384384
}
385385

386+
// Similar to the above, but with a more typical endpoint
387+
388+
public function testResourceAPIScaffoldsCorrectly()
389+
{
390+
$routes = $this->getCollector();
391+
$routes->setHTTPVerb('get');
392+
393+
$routes->resource('api/photos', ['controller' => 'Photos']);
394+
395+
$expected = [
396+
'api/photos' => '\Photos::index',
397+
'api/photos/new' => '\Photos::new',
398+
'api/photos/(.*)/edit' => '\Photos::edit/$1',
399+
'api/photos/(.*)' => '\Photos::show/$1',
400+
];
401+
402+
$this->assertEquals($expected, $routes->getRoutes());
403+
404+
$routes = $this->getCollector();
405+
$routes->setHTTPVerb('post');
406+
$routes->resource('api/photos', ['controller' => 'Photos']);
407+
408+
$expected = [
409+
'api/photos' => '\Photos::create',
410+
];
411+
412+
$this->assertEquals($expected, $routes->getRoutes());
413+
414+
$routes = $this->getCollector();
415+
$routes->setHTTPVerb('put');
416+
$routes->resource('api/photos', ['controller' => 'Photos']);
417+
418+
$expected = [
419+
'api/photos/(.*)' => '\Photos::update/$1',
420+
];
421+
422+
$this->assertEquals($expected, $routes->getRoutes());
423+
424+
$routes = $this->getCollector();
425+
$routes->setHTTPVerb('patch');
426+
$routes->resource('api/photos', ['controller' => 'Photos']);
427+
428+
$expected = [
429+
'api/photos/(.*)' => '\Photos::update/$1',
430+
];
431+
432+
$this->assertEquals($expected, $routes->getRoutes());
433+
434+
$routes = $this->getCollector();
435+
$routes->setHTTPVerb('delete');
436+
$routes->resource('api/photos', ['controller' => 'Photos']);
437+
438+
$expected = [
439+
'api/photos/(.*)' => '\Photos::delete/$1',
440+
];
441+
442+
$this->assertEquals($expected, $routes->getRoutes());
443+
}
444+
445+
public function testPresenterScaffoldsCorrectly()
446+
{
447+
$routes = $this->getCollector();
448+
$routes->setHTTPVerb('get');
449+
450+
$routes->presenter('photos');
451+
452+
$expected = [
453+
'photos' => '\Photos::index',
454+
'photos/show/(.*)' => '\Photos::show/$1',
455+
'photos/new' => '\Photos::new',
456+
'photos/edit/(.*)' => '\Photos::edit/$1',
457+
'photos/remove/(.*)' => '\Photos::remove/$1',
458+
'photos/(.*)' => '\Photos::show/$1',
459+
];
460+
461+
$this->assertEquals($expected, $routes->getRoutes());
462+
463+
$routes = $this->getCollector();
464+
$routes->setHTTPVerb('post');
465+
$routes->presenter('photos');
466+
467+
$expected = [
468+
'photos/create' => '\Photos::create',
469+
'photos/update/(.*)' => '\Photos::update/$1',
470+
'photos/delete/(.*)' => '\Photos::delete/$1',
471+
'photos' => '\Photos::create',
472+
];
473+
474+
$this->assertEquals($expected, $routes->getRoutes());
475+
}
476+
386477
//--------------------------------------------------------------------
387478

388479
public function testResourcesWithCustomController()

user_guide_src/source/incoming/restful.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,16 @@ Its usage is similar to the resosurce routing::
139139

140140
// Equivalent to the following:
141141
$routes->get('photos', 'Photos::index');
142+
$routes->post('photos', 'Photos::create'); // alias
142143
$routes->get('photos/show/(:segment)', 'Photos::show/$1');
143144
$routes->get('photos/new', 'Photos::new');
144145
$routes->post('photos/create', 'Photos::create');
145146
$routes->get('photos/edit/(:segment)', 'Photos::edit/$1');
146147
$routes->post('photos/update/(:segment)', 'Photos::update/$1');
147148
$routes->get('photos/remove/(:segment)', 'Photos::remove/$1');
148149
$routes->post('photos/delete/(:segment)', 'Photos::update/$1');
149-
150+
$routes->get('photos/(:segment)', 'Photos::show/$1'); // alias
151+
150152
You would not have routes for `photos` for both a resource and a presenter
151153
controller. You need to distinguish them, for instance::
152154

0 commit comments

Comments
 (0)