GithubHelp home page GithubHelp logo

laravel-automap's Introduction

AutoMap for Laravel

If you've ever found yourself with a routes.php file crammed full of routes and wondered, "Hey, can I have a second routes file"? Well, sure, you can include it. You can even modify RoutesServiceProvider to iterate over an array of namespaces and routes files.

Or you can use AutoMapServiceProvider and have all the routes files you like.

How Does It Work?

AutoMapServiceProvider looks in your Controllers directory, finds any subdirectories, and checks to see if there's a Routes subdirectory with the same name. Then it maps the controllers to the routes, letting Laravel know they exist and where they are.

This means that if you have this folder structure:

app/Http/Controllers/Foo/FooController.php

app/Http/Routes/Foo/routes.php

these will be automatically mapped to each other and available to Laravel.

Installation

Place AutoMapServiceProvider.php in your App\Providers directory.

Modify config/app.php to include App\Providers\AutoMapServiceProvider::class in the 'providers' array. It may be useful to run composer dump-autoload after.

If you've changed your app's name via php artisan app:name, you'll have to change the namespacing above from App\... to YourAppName\.... You'll aso have to change the namespacing in the actual AutoMapServiceProvider.php file.

Adding controllers and routes

Take whatever portion of your routes you want to abstract away and move them to App/Routes/{controller}/routes.php. You will have to create the Routes directory, as it does not exist by default.

For example, your controller app/Http/Controllers/SuperSecret/SuperSecretController.php might look something like this:

namespace App\Http\Controllers\SuperSecret;
...
class SuperSecretController extends Controller
{
    public function hidden() {
        return "Security through obscurity";
    }
}

and your routes file app/Http/Routes/SuperSecret/routes.php might have this:

Route::get('super/secret/hidden', 'SuperSecretController@hidden')->name('some-name');

And that's it! You don't have to register the directory anywhere, you don't have to make any assocation betwen the two. It just works, and things like route names are available throughout your application. You also don't have to prepend the controller with its subdirectory like you would using Laravel's base controller.

Notes

You can use this alongside Laravel's RouteServiceProvider. However if you want to completely replace it, just add your default controller namespacing and routes file location to the automaps array and remove RouteServiceProvider from config/app.php. The autoMap() method appends to this array so you'll be fine.

Contributing

Yes please. Go ahead. Tell me if I'm doing something silly, or if there if I'm duplicating secret functionality that Laravel already has.

laravel-automap's People

Contributors

danielsdeboer avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.