2

我使用 Laravel 5。

我试试,

 "use Illuminate\Contracts\Routing\Middleware;"

将“中间件”实现为,

class Language implements Middleware {
       // Some Functions 
  }

我得到错误,

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

实际上那个接口是 Missing 吗?

(或)定义错误?

(或)需要创建 | 下载 ?

谢谢问!

4

3 回答 3

6

Illuminate\Contracts\Routing\Middleware 合约在 5.2 中已被弃用,请将其删除。并且不要在类定义中使用它。

像这样

<?php namespace App\Http\Middleware;

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

class Language{
    //..... YOUR CODE
}
于 2016-02-18T16:57:03.367 回答
1

我不知道您是否真的放置了那些双引号,但这是一个工作示例:

<?php

use Illuminate\Contracts\Routing\Middleware;

class Language implements Middleware {
   // Some Functions 
}
于 2016-02-17T06:37:30.057 回答
0

我的课 :

命名空间应用\Http\中间件;

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

于 2016-02-17T06:48:52.157 回答