一切似乎都正常,但 VSCODE 在尝试时不断报告错误。说方法还没有定义。
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
class RegisterController extends Controller
{
public function index()
{
return view('auth.register');
}
public function store(Request $request)
{
//validation
$this->validate($request, [
'name' => 'required|max:50',
'username' => 'required|max:50',
'email' => 'required|email|max:250',
'password' => 'required|confirmed',
]);
//create new user
User::create([
'name' => $request->name,
'username' => $request->username,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
auth()->attempt($request->only('email','password'));
return redirect()->route('dashboard');
//sign the user in
//redirect
}
}
是 vscode 的问题还是我遗漏了什么?
所以我想我遵循了 TraversyMedia 的嘉宾 Alex 的相同教程,并偶然发现了同样的问题。
虽然抑制诊断显然有效,但这并不完全是我想要的(不会接受)。
在这种情况下,方法
attempt
存在,所以我只希望很棒的 Intelephense 扩展能够识别它。
我会尽力解释,以便它不仅仅适用于这种情况。
Ctrl + click
上的 auth()
应该具有该方法,将我们带到定义函数的 helpers.php
文件。
我们看到一小段带有
if
语句的代码,但无论哪种方式,它都会返回相同的类,即 AuthFactory
。
所以我们再次
Ctrl + click
在AuthFactory
上,我们发现自己盯着Factory
命名空间的接口Illuminate\Contracts\Auth
;
现在我们可以使用 PHPDocs 告诉 Intelephense 可以在此接口的实例上使用名为
attempt
的方法。请注意下面完整代码中interface Factory
声明上方的“注释”(PHPDocs)。
<?php
namespace Illuminate\Contracts\Auth;
/**
* @method attempt
*/
interface Factory
{
/**
* Get a guard instance by name.
*
* @param string|null $name
* @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard
*/
public function guard($name = null);
/**
* Set the default guard the factory should serve.
*
* @param string $name
* @return void
*/
public function shouldUse($name);
}
瞧!
只有当该方法确实存在时才应该这样做,否则你就是在自毁棺材。
这是我解决的:
打开扩展设置:
并打开设置的json
您应该考虑的变量是:
"intelephense.diagnostics.undefinedTypes": false,
"intelephense.diagnostics.undefinedFunctions": false,
"intelephense.diagnostics.undefinedConstants": false,
"intelephense.diagnostics.undefinedClassConstants": false,
"intelephense.diagnostics.undefinedMethods": false,
"intelephense.diagnostics.undefinedProperties": false,
"intelephense.diagnostics.undefinedVariables": false,
然后按
control + shift+p
并搜索 intelephense
,然后选择 index worksapce