Laravel 5 | Interface 'Illuminate\\Contracts\\

2019-07-08 03:02发布

问题:

I use Laravel 5.

I try,

 "use Illuminate\Contracts\Routing\Middleware;"

to implement "Middleware" as,

class Language implements Middleware {
       // Some Functions 
  }

I Get Error as,

Interface 'Illuminate\Contracts\Routing\Middleware' not found

Is actually that interface is Missing ?

(OR) Mistake in defining ?

(OR) Need to Create | Download ?

Thank Q !

回答1:

The Illuminate\Contracts\Routing\Middleware contract has been deprecated in 5.2, remove it. And dont use it in class definition.

Like this

<?php namespace App\Http\Middleware;

use Closure;
use Illuminate\Routing\Redirector;
use Illuminate\Http\Request;
use Illuminate\Foundation\Applicaion;

class Language{
    //..... YOUR CODE
}


回答2:

I dont know if you realy placed those double quote, but here is a working example:

<?php

use Illuminate\Contracts\Routing\Middleware;

class Language implements Middleware {
   // Some Functions 
}


回答3:

My Class :

namespace App\Http\Middleware;

use Closure;

use Illuminate\Routing\Redirector;
use Illuminate\Http\Request;
use Illuminate\Foundation\Application;
use Illuminate\Contracts\Routing\Middleware;

class Language implements Middleware {

public function __construct(Application $app, Redirector $redirector, Request $request) {
    $this->app = $app;
    $this->redirector = $redirector;
    $this->request = $request;
}

/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
public function handle(Closure $next, $request)
{
    // Make sure current locale exists.
    $locale = $request->segment(1);

    if ( ! array_key_exists($locale, $this->app->config->get('app.locales'))) {
        $segments = $request->segments();
        $segments[0] = $this->app->config->get('app.fallback_locale');

        return $this->redirector->to(implode('/', $segments));
    }

    $this->app->setLocale($locale);

    return $next($request);
}

 }

LOC : App/Http/Middleware/Language.php