PHP7/Laravel - 声明,应与兼容
PHP7/Laravel - Declaration of, Should be compatible with
我正在尝试恢复和更新旧的 Laravel 项目,目前面临一个问题。
我们收到的错误是在 MessagesController 内部的第 11 行抛出的,如下所示:“…/app/Http/Controllers/Administrator/MessagesController.php:11”
Declaration of
App\Http\Controllers\Administrator\MessagesController::getUsername(Illuminate\Http\Request $request)
should be compatible with
App\Http\Controllers\Controller::getUserName(?App\Http\Controllers\Request $request = NULL)
消息控制器:
namespace App\Http\Controllers\Members;
use App\Http\Controllers\Controller;
use App\Message;
use App\User;
use Illuminate\Http\Request;
use DB;
use Response;
class MessagesController extends Controller
{
//trimmed for Whosebug
public function getUsername(Request $request)
{
$term = $request->input('term');
$results = array();
$queries = DB::table('users')->where('username', 'LIKE', '%' . $term . '%')
->take(5)
->get();
foreach ($queries as $query) {
$results[] = [
'id' => $query->id,
'value' => $query->username
];
}
return Response::json($results);
}
}
基地控制器:
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Sentinel;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
protected function getUser()
{
$user = Sentinel::getUser();
return $user;
}
protected function getUserName(Request $request = null)
{
return $this->getUser()->username ?? null;
}
protected function getUserID()
{
return $this->getUser()->id ?? null;
}
}
您的parentController
没有包含use Illuminate\Http\Request;
。
其次,您的 parent getUserName 方法将请求参数设置为可为空。所以把你的 MessagesController
改成这个。
public function getUsername(?Request $request)
{
此错误也可能是由于控制器中具有相同名称的方法造成的。例如,
class Controller extends BaseController
{
// use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function **contactUs**(?Request $request){
(new MailingController())->**contactUs**($email, $full_name);
return response()->json([
'response'=>'Thank you for contacting us, we will reach back to you shourtly',
'status'=>'200'
],
200
);
}
}
经过多次调试,我通过重命名 contactUs 方法之一解决了这个问题。很奇怪,但这解决了我的问题
我正在尝试恢复和更新旧的 Laravel 项目,目前面临一个问题。 我们收到的错误是在 MessagesController 内部的第 11 行抛出的,如下所示:“…/app/Http/Controllers/Administrator/MessagesController.php:11”
Declaration of
App\Http\Controllers\Administrator\MessagesController::getUsername(Illuminate\Http\Request $request)
should be compatible with
App\Http\Controllers\Controller::getUserName(?App\Http\Controllers\Request $request = NULL)
消息控制器:
namespace App\Http\Controllers\Members;
use App\Http\Controllers\Controller;
use App\Message;
use App\User;
use Illuminate\Http\Request;
use DB;
use Response;
class MessagesController extends Controller
{
//trimmed for Whosebug
public function getUsername(Request $request)
{
$term = $request->input('term');
$results = array();
$queries = DB::table('users')->where('username', 'LIKE', '%' . $term . '%')
->take(5)
->get();
foreach ($queries as $query) {
$results[] = [
'id' => $query->id,
'value' => $query->username
];
}
return Response::json($results);
}
}
基地控制器:
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Sentinel;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
protected function getUser()
{
$user = Sentinel::getUser();
return $user;
}
protected function getUserName(Request $request = null)
{
return $this->getUser()->username ?? null;
}
protected function getUserID()
{
return $this->getUser()->id ?? null;
}
}
您的parentController
没有包含use Illuminate\Http\Request;
。
其次,您的 parent getUserName 方法将请求参数设置为可为空。所以把你的 MessagesController
改成这个。
public function getUsername(?Request $request)
{
此错误也可能是由于控制器中具有相同名称的方法造成的。例如,
class Controller extends BaseController
{
// use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function **contactUs**(?Request $request){
(new MailingController())->**contactUs**($email, $full_name);
return response()->json([
'response'=>'Thank you for contacting us, we will reach back to you shourtly',
'status'=>'200'
],
200
);
}
}
经过多次调试,我通过重命名 contactUs 方法之一解决了这个问题。很奇怪,但这解决了我的问题