当前正面临旧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 Stackoverflow
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;
}
}
答案 0 :(得分:1)
您的父母Controller
未包含use Illuminate\Http\Request;
。
第二,您的父getUserName方法将request参数设置为可为空。因此,将您的MessagesController
更改为此。
public function getUsername(?Request $request)
{
答案 1 :(得分:0)
此错误也可能是由于控制器中存在同名方法所致。例如,
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 方法之一解决了这个问题。很奇怪,但这解决了我的问题