PHP code example of silvertipsoftware / rest-router

1. Go to this page and download the library: Download silvertipsoftware/rest-router library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

silvertipsoftware / rest-router example snippets


URL::mixin(new \SilvertipSoftware\RestRouter\UrlMixins);

Route::resource('posts', 'PostsController');

$post = Post::find(123);
$url = URL::url($post); 
// $url = https://mysite.com/posts/123

$path = URL::path(Post::class);
// $path = /posts
$path = URL::path(new Post);
// $path = /posts

$url = URL::url($post, 'edit');
// $url = https://mysite.com/posts/123/edit

$path = URL::path(Post::class, 'create');
// $path = /posts/create

Route::resource('logs', 'LogsController')->only(['store', 'destroy']);


// the 'logs.index' route doesn't need to be defined:
$path = URL::path(Log::class);
// $path = /logs

// the 'logs.show' route doesn't need to be defined:
$path = URL::path($logEntry);
// $path = /logs/123

Route::prefix('backoffice')->name('admin.')->group(function () {
  Route::resource('logs', 'LogsController');
});


$path = URL::path('admin', Log::class);
// $path = /backoffice/logs

$path = URL::path('backoffice', Log::class); // errors

Route::get('/posts/{post}/metadata', 'PostsController@metadata')->name('posts.metadata');


$path = URL::path($post, ['action' => 'metadata']);
// $path = /posts/123/metadata

RestRouter::$shallowResources = false;

Route::resource('posts.comments', 'CommentsController');


$path = URL::path($post, $comment, ['shallow' => false]);
// $path = /posts/123/comments/456

$options = [
  'shallow' => false, // defaults to true
  'action' => 'edit', // RESTful actions 'create', 'edit' etc. or any other action you've defined
  'format' => 'json' // added as an extension to the uri, can be anything
]

Route::resource('posts', 'PostsController');
Route::get('/{account_id}/logs/{log}')->name('logs.show');


$path = URL::path($log, ['account_id'=>111]);
// $path = /111/logs/123

$url = URL::url($post, ['action' => 'edit']); 
// $url = https://mysite.com/posts/456/edit

$path = URL::path($post, ['format' => 'json']);
// $path = /posts/456.json

$path = URL::path($post, ['mode' => 'full']);
// $path = /posts/456?mode=full