PHPackages                             marcot89/laravel-magic-routes - 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. [Framework](/categories/framework)
4. /
5. marcot89/laravel-magic-routes

ActiveLibrary[Framework](/categories/framework)

marcot89/laravel-magic-routes
=============================

Magic Routes for Laravel Applications

v0.5.0(5y ago)523MITPHPCI failing

Since Mar 29Pushed 5y ago2 watchersCompare

[ Source](https://github.com/marcoT89/laravel-magic-routes)[ Packagist](https://packagist.org/packages/marcot89/laravel-magic-routes)[ Docs](https://github.com/marcot89/laravel-magic-routes)[ RSS](/packages/marcot89-laravel-magic-routes/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (7)Dependencies (3)Versions (8)Used By (0)

LaravelMagicRoutes
==================

[](#laravelmagicroutes)

[![Latest Version on Packagist](https://camo.githubusercontent.com/17fa77b2b4f11bd0dd7cc9fe8592b1a27c3c120be07adf7b8faac4e3cedebef4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6172636f7438392f6c61726176656c2d6d616769632d726f757465732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/marcot89/laravel-magic-routes)[![Total Downloads](https://camo.githubusercontent.com/99b0c87c60b425799b20e73cae47259db566f5846354bd1f4b1e034f5f4b0776/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d6172636f7438392f6c61726176656c2d6d616769632d726f757465732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/marcot89/laravel-magic-routes)

Create RESTfull routes for your laravel app based on your controller names and namespaces to avoid overrides and define a pattern to your projects routes. It is compatible with laravel 7+.

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

[](#installation)

Via Composer

```
$ composer require marcot89/laravel-magic-routes
```

Usage
-----

[](#usage)

Add `MagicRoutes` trait to the controller to generate routes for it automatically:

```
namespace App\Http\Controllers;

use MarcoT89\LaravelMagicRoutes\Traits\MagicRoutes;

class UserController extends Controller
{
    use MagicRoutes;
}
```

For now there is no route for your controller because you don't have any `public` method declared on it. A **new route** will be generated for every **public method** added to the controller. So if we add the common crud public methods like this:

```
namespace App\Http\Controllers;

class UserController extends Controller
{
    use MagicRoutes;

    public function index(...) {...}
    public function store(...) {...}
    public function update(...) {...}
    public function create(...) {...}
    public function show(...) {...}
    public function edit(...) {...}
    public function destroy(...) {...}
    public function forceDestroy(...) {...}
}
```

We will have these generated routes based on those public methods:

```
| GET|HEAD | users                      | users.index          | App\Http\Controllers\UserController@index          |
| POST     | users                      | users.store          | App\Http\Controllers\UserController@store          |
| GET|HEAD | users/create               | users.create         | App\Http\Controllers\UserController@create         |
| PUT      | users/{user}               | users.update         | App\Http\Controllers\UserController@update         |
| GET|HEAD | users/{user}               | users.show           | App\Http\Controllers\UserController@show           |
| DELETE   | users/{user}               | users.destroy        | App\Http\Controllers\UserController@destroy        |
| GET|HEAD | users/{user}/edit          | users.edit           | App\Http\Controllers\UserController@edit           |
| DELETE   | users/{user}/force-destroy | users.force-destroy  | App\Http\Controllers\UserController@forceDestroy   |

```

### \# Customize Http Methods for Routes

[](#-customize-http-methods-for-routes)

Any other public method in the controller will be generated a route with a GET http method as default, but you can customize it with a prefix. Let's see an example.

```
class PostController extends Controller
{
    use MagicRoutes;

    // Generated route:
    // GET /posts/{post}/publish
    public function publish(Post $post) {...}

    // Generated route:
    // POST /posts/{post}/publish
    public function postPublish(Post $post) {...}

    // Generated route:
    // PUT /posts/{post}/publish
    public function putPublish(Post $post) {...}

    // Generated route:
    // DELETE /posts/{post}/publish
    public function deletePublish(Post $post) {...}
}
```

### \# Route Params

[](#-route-params)

By convention the first parameter will be set before the action name. All other parameters will be added after.

```
class PostController extends Controller
{
    public function publish(Post $post, $one, $two, $three) {...}
}
```

Will generate:

```
GET /posts/{post}/publish/{one}/{two}/{three}

```

### \# Middlewares

[](#-middlewares)

There are two ways to declare a middleware for a controller:

**Using Protected Property**

```
class UserController extends Controller
{
    use MagicRoutes;

    // use a string for one middlware
    protected $middleware = 'auth';
    // or use an array for many middlewares
    protected $middleware = [
        'auth',
        'verified' => ['except' => ['index', 'edit', 'update']],
    ];
}
```

**Using Constructor**

```
class UserController extends Controller
{
    use MagicRoutes;

    public function __construct()
    {
        $this->middleware('auth');
    }
}
```

### \# Namespaced Routes

[](#-namespaced-routes)

The controller namespace will generate a prefix for that route:

```
namespace App\Http\Controllers\Api\V1;

...

class UserController extends Controller
{
    use MagicRoutes;

    public function index(...) {...}
}
```

Any public method declared in this controller will generate urls with prefix like:

```
/api/v1/users

```

And named routes like:

```
api.v1.users

```

### \# Invokable Controllers

[](#-invokable-controllers)

If you like to create a controller for every action you can use the invokable controllers in a namespaced that makes sense for your route. Example:

```
namespace App\Http\Controllers\Posts;

class PublishController extends Controller
{
    use MagicRoutes;

    protected $middleware = 'auth';
    protected $method = 'post'; // only works for invokable controllers
    // or
    protected $method = 'post|put'; // separate by pipe for more http methods

    public function __invoke(Post $post) {...}
}
```

This will generate an URL like:

```
POST /posts/publish/{post} -> name: posts.publish

```

### \# Nested RESTfull Routes

[](#-nested-restfull-routes)

Sometimes you want or need to define nested resources. Let's say we have posts of a user but we want nested routes for that. You can do it with a namespaced controller like this:

```
namespace App\Http\Controllers\Users; // Note that it is inside Users namespace for prefix route

class PostController extends Controller
{
    use MagicRoutes;

    protected $prefix = '{user}';

    public function index(User $user) {
        return $user->posts()->paginate();
    }
}
```

This will generate the following route:

```
GET /users/{user}/posts -> name: users.posts.index

```

### \# Resource URLs Plural vs Singular

[](#-resource-urls-plural-vs-singular)

RESTfull resources is always in plural. So the convention defines URL resources in plural no matter the name of the controller. If your controller is `UserController` or `UsersController` both will generate the same resource URL `/users`.

But you can disable this behavior using the `plural` property:

```
class UserController extends Controller
{
    use MagicRoutes;

    protected $plural = false;

    ...
}
```

With this property `false` will generate routes following the controller name. Now if your controller is `UserController` it will generate a route `/user`. If it is `UsersController` it will generate `/users`.

> **Note:** For invokable controllers the plural property is always disabled.

Change log
----------

[](#change-log)

Please see the [changelog](changelog.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [contributing.md](contributing.md) for details and a todolist.

Security
--------

[](#security)

If you discover any security related issues, please email author email instead of using the issue tracker.

Credits
-------

[](#credits)

- [Marco Avila](https://github.com/marcot89)
- [All Contributors](../../contributors)

MIT License
-----------

[](#mit-license)

Please see the [license file](license.md) for more information.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~12 days

Recently: every ~18 days

Total

7

Last Release

2161d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c6928b4b29bffdea8fab83134c026cbf09878c7a90490ddc7ec4d430dfd9e6d7?d=identicon)[marcoT89](/maintainers/marcoT89)

---

Top Contributors

[![marcotas](https://avatars.githubusercontent.com/u/3236791?v=4)](https://github.com/marcotas "marcotas (14 commits)")

---

Tags

laravelLaravelMagicRoutes

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/marcot89-laravel-magic-routes/health.svg)

```
[![Health](https://phpackages.com/badges/marcot89-laravel-magic-routes/health.svg)](https://phpackages.com/packages/marcot89-laravel-magic-routes)
```

###  Alternatives

[laravel/socialite

Laravel wrapper around OAuth 1 &amp; OAuth 2 libraries.

5.7k96.9M674](/packages/laravel-socialite)[laravel/horizon

Dashboard and code-driven configuration for Laravel queues.

4.2k84.2M225](/packages/laravel-horizon)[laravel/ui

Laravel UI utilities and presets.

2.7k134.9M601](/packages/laravel-ui)[laravel/sail

Docker files for running a basic Laravel application.

1.9k186.9M1.0k](/packages/laravel-sail)[laravel/jetstream

Tailwind scaffolding for the Laravel framework.

4.1k19.8M136](/packages/laravel-jetstream)[laravel/dusk

Laravel Dusk provides simple end-to-end testing and browser automation.

1.9k36.7M259](/packages/laravel-dusk)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
