PHPackages                             silvertipsoftware/rest-router - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. silvertipsoftware/rest-router

ActiveLibrary[API Development](/categories/api)

silvertipsoftware/rest-router
=============================

Smart url construction using classes and models

v0.1.2(5y ago)07.3k[2 issues](https://github.com/SilvertipSoftware/RestRouter/issues)1MITPHP

Since Oct 15Pushed 5y ago2 watchersCompare

[ Source](https://github.com/SilvertipSoftware/RestRouter)[ Packagist](https://packagist.org/packages/silvertipsoftware/rest-router)[ Docs](https://github.com/SilvertipSoftware/RestRouter)[ RSS](/packages/silvertipsoftware-rest-router/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (1)Versions (8)Used By (1)

RestRouter
==========

[](#restrouter)

About
-----

[](#about)

The `rest-router` package allows you to construct urls by passing in an Eloquent model or corresponding class name.

Features
--------

[](#features)

- Smart url construction using classes and models.

Installation
------------

[](#installation)

Require the `silvertipsoftware/rest-router` package in your `composer.json` and update your dependencies:

```
$ composer require silvertipsoftware/rest-router
```

The package can be used by directly calling `url()` or `path()` on `SilvertipSoftware\RestRouter\RestRouter`, but for convenience, a mixin for Laravel's built-in `URL` facade is provided. To use, in your `RouteServiceProvider::boot` method, call:

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

The `url()` and `path()` methods will now be available on `URL`.

Usage
-----

[](#usage)

`RestRouter` is built primarily to work on routes defined with the `Route::resource()` method of Laravel's router, although hand-rolled routes can also be used. Behind the scenes, `RestRouter`uses the route names to create urls; the actual text of the url is irrelevant.

### Simple Resource Routes

[](#simple-resource-routes)

Assume that the following resource has been defined:

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

Then in your code you can do:

```
$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
```

### Fallbacks

[](#fallbacks)

Not every REST action need be defined on a resource. `RestRouter` will check other route names which generate the same url, since it is not concerned with the HTTP method used. For example:

```
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
```

### Prefixed Resource Routes

[](#prefixed-resource-routes)

`RestRouter` needs to be given any route name prefixes (*not* uri prefixes) defined. For example:

```
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
```

### Non-REST Actions

[](#non-rest-actions)

If you've defined non-typical REST actions on your resource, you can pass an `action` option to `RestRouter` to find that route name. For example:

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

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

### Nested Resources

[](#nested-resources)

`RestRouter` assumes shallow resources by default. If you use nested resources, this can be enabled globally or on a per-route basis.

For enabling nested resource support globally, in your `RouteServiceProvider` insert:

```
RestRouter::$shallowResources = false;
```

On a per-route basis, pass a `"shallow" => false` option to your call to `url()` or `path()` as below. For example:

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

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

### Route Generation Options

[](#route-generation-options)

If the last argument is an array, it is treated as a keyed array of options to modify the url. Available options are:

```
$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
]
```

Any other keys added to the `$options` parameter are treated as either query parameters for the generated url, or as route parameters if they are defined.

```
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
```

Also works in blade templates:

```
