我使用Laravel 5.
我试试,
"use Illuminate\Contracts\Routing\Middleware;"
将“中间件”实现为,
class Language implements Middleware {
// Some Functions
}
我得到错误,
Interface 'Illuminate\Contracts\Routing\Middleware' not found
实际上该界面是否缺失?
(或)错误定义?
(或)需要创建|下载?
谢谢Q!
答案 0 :(得分:5)
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
}
答案 1 :(得分:1)
我不知道你是否真的放了那些双引号,但这是一个有效的例子:
<?php
use Illuminate\Contracts\Routing\Middleware;
class Language implements Middleware {
// Some Functions
}
答案 2 :(得分:0)
我的班级:
命名空间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