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

Router Unslash Laravel Package

amnl/router-unslash

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require amnl/router-unslash
    

    (Note: Due to Symfony 2.3 dependency, ensure compatibility with your Laravel project via Symfony Bridge or consider alternatives like spatie/laravel-honeypot for modern Laravel.)

  2. Publish Config

    php artisan vendor:publish --provider="AMNL\RouterUnslashBundle\RouterUnslashBundle" --tag="config"
    

    (Edit config/amnl_router_unslash.php to adjust permanent, public, and cache settings.)

  3. First Use Case

    • Problem: Users accessing /about or /about/ return 404s.
    • Solution: Add to routes/web.php:
      use AMNL\RouterUnslashBundle\RouterUnslashBundle;
      // Register bundle (if using Symfony Bridge)
      $app->register(new RouterUnslashBundle());
      
    • Result: Automatic redirects from /about/about/ (or vice versa) with configurable HTTP status (301/302).

Implementation Patterns

Core Workflows

  1. Route Normalization

    • Pattern: Apply to all public routes by wrapping RouteServiceProvider:
      public function boot()
      {
          parent::boot();
          $this->app->make('router')->getCompiler()->getRouteCollector()->getRouteCompiler()->setNormalizer(
              new AMNL\RouterUnslashBundle\Normalizer\UnslashNormalizer()
          );
      }
      
    • Use Case: Ensures /posts/1 and /posts/1/ resolve to the same handler.
  2. Conditional Activation

    • Pattern: Disable for API routes (e.g., /api/*) via middleware:
      $router->pushMiddlewareToGroup('web', \AMNL\RouterUnslashBundle\HttpFoundation\UnslashMiddleware::class);
      
    • Tip: Exclude admin routes by prefixing with admin/ and adding to ignored_paths in config.
  3. Cache Optimization

    • Pattern: Leverage public: true and maxage for CDN-friendly redirects:
      'amnl_router_unslash' => [
          'permanent' => env('PROD') ? true : false, // 301 in production
          'maxage' => 3600, // 1-hour cache for non-permanent redirects
      ],
      
    • Use Case: Reduce server load for repeated slash-mismatched requests.

Integration Tips

  • Laravel-Specific: Use app('router')->getRouteCollector()->getCompiler()->getRouteCompiler()->setNormalizer() in AppServiceProvider@boot().
  • Testing: Mock redirects in PHPUnit:
    $this->get('/about')->assertRedirect('/about/');
    
  • SEO: Set permanent: true in production to pass link equity to canonical URLs.

Gotchas and Tips

Pitfalls

  1. Symfony 2.3 Dependency

    • Issue: Incompatible with Laravel ≥5.8. Use Symfony Bridge or fork the package to replace Symfony components with Laravel equivalents.
    • Workaround: Replace RouterUnslashBundle with laravel-slash (modern alternative).
  2. Overzealous Redirects

    • Issue: Redirects may conflict with existing route logic (e.g., /admin vs /admin/).
    • Fix: Exclude paths in config:
      'ignored_paths' => [
          'admin/*',
          'api/*',
      ],
      
  3. Cache Headers

    • Issue: public: true may expose redirects to CDNs prematurely.
    • Tip: Use smaxage for shared caches and maxage for private browsers.

Debugging

  • Log Redirects: Enable Symfony’s profiler or Laravel’s dd() in UnslashMiddleware to trace unexpected redirects.
  • Check Status Codes: Use curl -v http://example.com/about to verify 301/302 responses.

Extension Points

  1. Custom Normalizers

    • Extend UnslashNormalizer to handle edge cases (e.g., query strings):
      class CustomNormalizer extends UnslashNormalizer {
          protected function normalizePath($path) {
              return parent::normalizePath(rtrim($path, '?'));
          }
      }
      
  2. Dynamic Config

    • Override config per environment:
      config(['amnl_router_unslash.permanent' => env('APP_ENV') === 'production']);
      
  3. Event Listeners

    • Hook into RouterUnslashEvents (if extended) to log redirects or block specific paths.

Pro Tips

  • Combine with Honeypot: Use spatie/laravel-honeypot to block slash-spam bots while keeping redirects clean.
  • Monitor 404s: Pair with laravel-debugbar to audit unhandled slash variations.
  • Document Canonical URLs: Add <link rel="canonical"> to templates to reinforce SEO consistency.
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.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope