Auto Route Generating (Auto-Discovery) Package for Laravel.

Overview

Laravel Auto Routes

                _              _____             _            
     /\        | |            |  __ \           | |           
    /  \  _   _| |_ ___ ______| |__) |___  _   _| |_ ___  ___ 
   / /\ \| | | | __/ _ \______|  _  // _ \| | | | __/ _ \/ __|
  / ____ \ |_| | || (_) |     | | \ \ (_) | |_| | ||  __/\__ \
 /_/    \_\__,_|\__\___/      |_|  \_\___/ \__,_|\__\___||___/

Total Downloads Latest Stable Version Latest Unstable Version License

Automatically Route Generator Package for Laravel.

Install

Supported Laravel Versions: >= 6.x

Run the following command directly in your Project path:

$ composer require izniburak/laravel-auto-routes

The service provider of the Package will be automatically discovered by Laravel.

After that, you should publish the config file via following command:

$ php artisan vendor:publish --provider="Buki\AutoRoute\AutoRouteServiceProvider"

Greate! You can start to use Auto Route Package.

Usage

Open web.php or api.php files in routes directory, and add a new route that will be generated automatically:

Route::auto('/test', 'TestController');

All methods will be automatically generated by the AutoRoute Package.

Details

  • You can use auto-route.php file in config directory in order to change configuration of the Package. You can;

    • add new patterns for the parameters of the methods.
    • change default HTTP methods.
    • change main method.
  • You can use AutoRouteFacade in web.php and api.php in order to simple code completion for the method while using an IDE or Editor. You can add the following line to top of the file:

use Buki\AutoRoute\AutoRouteFacade as Route;
  • All methods which will be auto generated must have public accessor to discovered by the AutoRoute Package.

  • If you use camelCase style for your method names in the Controllers, these methods endpoints will automatically convert to kebab-case to make pretty URLs. For example:

Route::auto('/test', 'TestController');
# OR
Route::auto('/test', TestController::class);
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
    /**
     * URL will be converted to "/test/foo-bar"
     */
    public function fooBar(Request $request)
    {
        // your codes
    }
}
  • You can specify HTTP Method for the method of the Controllers. If you want that a method works with GET method and other method works with POST method, you can do it. Just add a prefix for the method. That's all. For example;
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
    /**
     * URL: "/test/foo-bar"
     * This method will only work with 'GET' method. 
     */
    public function getFooBar(Request $request)
    {
        // your codes
    }
    
    /**
     * URL: "/test/bar-baz"
     * This method will only work with 'POST' method. 
     */
    public function postBarBaz(Request $request)
    {
        // your codes
    }
}
  • If you don't add any prefix to your methods to use HTTP method definition, all URL will work with all HTTP methods. This options can be changed from auto-route.php configuration file.

  • If you want to use snake_case format for your methods, you can do it like that:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
    /**
     * URL: "/test/foo_bar"
     * This method will only work with 'GET' method. 
     */
    public function get_foo_bar(Request $request)
    {
        // your codes
    }
    
    /**
     * URL: "/test/bar_baz"
     * This method will only work with 'POST' method. 
     */
    public function post_bar_baz(Request $request)
    {
        // your codes
    }
}
  • You can add route options via third parameter of the auto method.
Route::auto('/test', 'TestController', [
    // your options... 
]);

Options array may contain all Laravel route attributes like name, middleware, namespace, etc..

In addition, you can add patterns into the Options array in order to define new patterns for the parameters of the methods in the Controllers. For example:

Route::auto('/test', 'TestController', [
    'name' => 'test',
    'middleware' => [YourMiddleware::class],
    'patterns' => [
        'id' => '\d+',
        'value' => '\w+',
    ],
]);

According to example above, you can use $id and $value parameters in all methods in the Controller. And for these parameters, the rules you defined will be applied.

Also, to define default patterns for the parameters, you can modify patterns in auto-route.php file.

  • You can specify the Routes which will be generated automatically by using only or except with options parameters. You should use method names in the Controllers. For example;
# First Example
Route::auto('/foo', 'FooController', [
    'only' => ['fooBar', 'postUpdatePost'],
]);

# Second Example
Route::auto('/bar', 'BarController', [
    'except' => ['test', 'putExample'],
]);

According to first example above, only two methods will be generated. And according to other example, all methods will be generated except two methods which specified.

  • If you don't change the main_method in configurations, your main method will be index for the Controllers. That's mean, you should be add index method into your controller to define base endpoint of the Controller. For example;
Route::auto('/test', 'TestController');
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
    /**
     * URL: "/test"
     */
    public function index(Request $request)
    {
        // your codes
    }
    
    /**
     * URL: "/test/foo-bar"
     */
    public function fooBar(Request $request)
    {
        // your codes
    }
}
  • You can use parameters as required and optional for the methods in your Controllers. For example;
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
    /**
     * URL: "/test/{id}"
     */
    public function index(Request $request, $id)
    {
        // your codes
    }
    
    /**
     * URL: "/test/foo-bar/{name}/{surname?}"
     */
    public function fooBar(Request $request, $name, $surname = null)
    {
        // your codes
    }
}

Also, you can use parameter type to use compatible pattern for the parameter. Parameter types can be int, string, float and bool. For example:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
    /**
     * URL: "/test/{id}"
     * id parameter must be numeric.  
     */
    public function index(Request $request, int $id)
    {
        // your codes
    }
    
    /**
     * URL: "/test/foo-bar/{name}/{surname?}"
     * name and surname parameters must be string.
     */
    public function fooBar(Request $request, string $name, string $surname = null)
    {
        // your codes
    }
}

If you define patterns for these variable names in the auto-route.php configuration file, your definition will be used for the value checking.

To use int, float, string and bool patterns quickly for your parameters, you can use parameter type directly.

  • You can use subfolder definition for the Controllers. For example;
Route::auto('/test', 'Backend.TestController');
# OR
Route::auto('/test', 'Backend\\TestController');

Support

You can use Issues

izniburak's homepage

izniburak's twitter

Licence

MIT Licence

Contributing

  1. Fork it ( https://github.com/izniburak/laravel-auto-routes/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • izniburak İzni Burak Demirtaş - creator, maintainer
Issues
  • add custom type hint

    add custom type hint

    If there is another way, I apologize for this PR, but this contribution is to be able to add Model as type hint: file auto-route.php

    'patterns'     => [
        'slug'     => '([\w\-_]+)',
        'uuid'     => '([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})',
        'date'     => '([0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]))',
        "customer" => "App\Models\Customer",
    ],
    

    Captura de Tela 2021-05-23 às 12 54 34

    in controller

    public function getShow(Customer $customer){}
    
    opened by MarceloSantosCorrea 7
  • Same level view for more than 2 variables.

    Same level view for more than 2 variables.

    Using it this way makes variables written under one another more readable.

    opened by aligurbuz 1
  • Error publish

    Error publish

    image

    Estou tendo este erro ao tentar publicar o vendor

    opened by brenoriot 0
Releases(v1.2.0)
Owner
İzni Burak Demirtaş
buki. software engineer & full-stack developer. #java #php #js #go #react-native #react #docker #k8s
İzni Burak Demirtaş
Generate a PHP script for faster routing :rocket:

SwitchRoute Generating a PHP script for faster routing. The traditional way of routing uses regular expressions. This method was improved by FastRoute

Arnold Daniels 66 Jun 22, 2021
A lightweight and simple object oriented PHP Router

bramus/router A lightweight and simple object oriented PHP Router. Built by Bram(us) Van Damme (https://www.bram.us) and Contributors Features Support

Bramus! 723 Oct 13, 2021
PhpRouter is a powerful, lightweight, and very fast HTTP URL router for PHP projects.

PhpRouter PhpRouter is a powerful, lightweight, and very fast HTTP URL router for PHP projects. Some of the provided features: Route parameters Predef

Milad Rahimi 108 Oct 13, 2021
Pux is a fast PHP Router and includes out-of-box controller tools

Pux Pux is a faster PHP router, it also includes out-of-box controller helpers. 2.0.x Branch Build Status (This branch is under development) Benchmark

Yo-An Lin 1.3k Sep 30, 2021
Auto Route Generating (Auto-Discovery) Package for Laravel.

Laravel Auto Routes _ _____ _ /\ | | | __ \ | |

İzni Burak Demirtaş 142 Oct 6, 2021
route:menu gives you a beautiful route list which is friendly on smaller terminals and brings a few new features in.

Laravel Route Menu Your route:list, sir. route:menu gives you a beautiful route list which is friendly on smaller terminals and brings a few new featu

Craig Morris 60 May 24, 2021
:tada: Release 2.0 is released! Very fast HTTP router for PHP 7.1+ (incl. PHP8 with attributes) based on PSR-7 and PSR-15 with support for annotations and OpenApi (Swagger)

HTTP router for PHP 7.1+ (incl. PHP 8 with attributes) based on PSR-7 and PSR-15 with support for annotations and OpenApi (Swagger) Installation compo

Sunrise // PHP 133 Oct 14, 2021
Fast request router for PHP

FastRoute - Fast request router for PHP This library provides a fast implementation of a regular expression based router. Blog post explaining how the

Nikita Popov 4.5k Oct 16, 2021
Fast PSR-7 based routing and dispatch component including PSR-15 middleware, built on top of FastRoute.

Route This package is compliant with PSR-1, PSR-2, PSR-4, PSR-7, PSR-11 and PSR-15. If you notice compliance oversights, please send a patch via pull

The League of Extraordinary Packages 576 Oct 20, 2021
PHP routing (like laravel) (not complete yet)

PHP Router (under construction) This repository contains routing classes that enables you to define your routes similar to laravel 8 routes. Features

Kareem M. Fouad 5 Jun 9, 2021
Checks if a laravel route is vlaid

Laravel Route Checker Checks if your Laravel routes has valid controllers Installation The package should be installed as a dev dependency, as there i

Worksome 4 Oct 19, 2021
A fast & flexible router

Klein.php klein.php is a fast & flexible router for PHP 5.3+ Flexible regular expression routing (inspired by Sinatra) A set of boilerplate methods fo

null 2.6k Oct 16, 2021
PHP routing class. Lightweight yet flexible. Supports REST, dynamic and reversed routing.

AltoRouter AltoRouter is a small but powerful routing class, heavily inspired by klein.php. $router = new AltoRouter(); // map homepage $router->map(

Danny van Kooten 1.1k Oct 14, 2021
Toro is a PHP router for developing RESTful web applications and APIs.

Toro Toro is a PHP router for developing RESTful web applications and APIs. It is designed for minimalists who want to get work done. Quick Links Offi

Kunal Anand 1.2k Oct 9, 2021
Convention based routing for PHP

Croute Convention based routing for PHP based on Symfony components. Croute is great because: You don't need to maintain a routing table Promotes cons

Michael O'Connell 13 Oct 20, 2020
:bird: Simple PHP router

Macaw Macaw is a simple, open source PHP router. It's super small (~150 LOC), fast, and has some great annotated source code. This class allows you to

Noah Buscher 890 Oct 7, 2021
A PHP rewrite of HackRouter by Facebook

Hack-Routing Fast, type-safe request routing, parameter retrieval, and link generation. It's a port of hack-router By Facebook, Inc. Components HTTP E

Saif Eddin Gmati 25 Aug 2, 2021
A web router implementation for PHP.

Aura.Router Powerful, flexible web routing for PSR-7 requests. Installation and Autoloading This package is installable and PSR-4 autoloadable via Com

Aura for PHP 448 Sep 7, 2021