Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Routing Laravel Package

derafu/routing

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require derafu/routing
    

    Add the service provider to config/app.php:

    'providers' => [
        // ...
        Derafu\Routing\RoutingServiceProvider::class,
    ],
    
  2. Basic Route Definition:

    use Derafu\Routing\Router;
    
    $router = app(Router::class);
    $router->get('/hello', fn() => 'Hello, World!');
    
  3. First Use Case: Register routes in a dedicated file (e.g., routes/web.php) and bootstrap them in your AppServiceProvider:

    public function boot()
    {
        $router = app(Router::class);
        require __DIR__.'/../routes/web.php';
    }
    
  4. Dispatching Requests:

    $request = new \Symfony\Component\HttpFoundation\Request();
    $response = $router->dispatch($request);
    

Implementation Patterns

Core Workflows

  1. Plugin-Based Routing: Extend functionality via plugins (e.g., middleware, route groups):

    $router->plugin(new \Derafu\Routing\Plugin\MiddlewarePlugin());
    $router->group(['middleware' => 'auth'], function($router) {
        $router->get('/dashboard', fn() => 'Dashboard');
    });
    
  2. Dynamic Route Generation: Use named routes for URL generation:

    $router->get('/user/{id}', fn() => 'User Profile')->name('user.profile');
    $url = $router->url('user.profile', ['id' => 123]);
    
  3. Route Caching: Cache routes for performance (e.g., in AppServiceProvider):

    $router->cacheRoutes();
    
  4. Integration with Laravel: Replace Laravel’s router in AppServiceProvider:

    public function register()
    {
        $this->app->singleton(\Illuminate\Routing\Router::class, function($app) {
            return app(Derafu\Routing\Router::class);
        });
    }
    

Common Patterns

  • Route Groups:
    $router->group(['prefix' => 'admin'], function($router) {
        $router->get('/dashboard', fn() => 'Admin Dashboard');
    });
    
  • Route Parameters:
    $router->get('/post/{slug}', function($slug) {
        return "Post: $slug";
    });
    
  • HTTP Methods:
    $router->post('/submit', fn() => 'Form Submitted');
    $router->put('/update', fn() => 'Updated');
    

Gotchas and Tips

Pitfalls

  1. Plugin Conflicts: Ensure plugins are registered before defining routes that depend on them. Plugins modify router behavior globally.

  2. Route Overrides: Cached routes may not reflect real-time changes. Clear the cache after modifying routes:

    php artisan route:clear
    

    (If using Laravel’s Artisan; otherwise, manually clear the cache.)

  3. Middleware Misconfiguration: Plugins like MiddlewarePlugin require middleware to be registered in Laravel’s container. Verify middleware exists before use.

  4. Case Sensitivity: Route matching is case-sensitive by default. Use case_sensitive: false in route groups if needed:

    $router->group(['case_sensitive' => false], function($router) {
        $router->get('/Home', fn() => 'Homepage');
    });
    

Debugging Tips

  • Inspect Routes: Use $router->getRoutes() to dump all registered routes for debugging.
  • Error Handling: Wrap dispatch in a try-catch to handle 404s gracefully:
    try {
        $response = $router->dispatch($request);
    } catch (\Derafu\Routing\Exception\RouteNotFoundException $e) {
        $response = new \Symfony\Component\HttpFoundation\Response('Not Found', 404);
    }
    
  • Plugin Debugging: Enable debug mode for plugins (if supported) to log their behavior:
    $router->plugin(new \Derafu\Routing\Plugin\MiddlewarePlugin())->debug();
    

Extension Points

  1. Custom Plugins: Create plugins by implementing Derafu\Routing\Plugin\PluginInterface:

    class MyPlugin implements PluginInterface {
        public function apply(Router $router) {
            $router->on('before', fn() => logger()->info('Route called'));
        }
    }
    

    Register via:

    $router->plugin(new MyPlugin());
    
  2. Route Matching Logic: Override the match() method in a custom router class for bespoke matching (e.g., regex-based routes).

  3. Response Handling: Extend the router to support custom response objects or modify responses post-dispatch.

  4. Middleware Integration: Use the MiddlewarePlugin to attach Laravel middleware to routes:

    $router->group(['middleware' => ['auth', 'throttle:60']], function($router) {
        $router->get('/api', fn() => 'API Endpoint');
    });
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor