Laravel: auth()->尝试未定义的方法
Laravel: auth()->attempt undefined method
似乎一切正常,但 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 问题还是我遗漏了什么?
这是我解决的:
打开扩展设置:
并打开设置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
然后 select index worksapce
所以我想我跟踪了同一个 tutorial of TraversyMedia 的客人 Alex,并且偶然发现了同样的问题。
虽然抑制诊断显然有效,但这并不是我想要的(不会接受)。
在这种情况下,方法 attempt
存在,所以我只希望出色的 Intelephense 扩展能够识别它。
我会尽力解释,所以它不仅适用于这种情况。
解决方案
Ctrl + click
on auth()
应该有方法将我们带到定义函数的 helpers.php
文件。
我们看到一小段带有 if
语句的代码,但无论哪种方式 returns 都是相同的 class,即 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);
}
瞧!
备注
只有当该方法确实存在时才应该这样做,否则你就是在给自己钉棺材。
似乎一切正常,但 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 问题还是我遗漏了什么?
这是我解决的:
打开扩展设置:
并打开设置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
然后 select index worksapce
所以我想我跟踪了同一个 tutorial of TraversyMedia 的客人 Alex,并且偶然发现了同样的问题。
虽然抑制诊断显然有效,但这并不是我想要的(不会接受)。
在这种情况下,方法 attempt
存在,所以我只希望出色的 Intelephense 扩展能够识别它。
我会尽力解释,所以它不仅适用于这种情况。
解决方案
Ctrl + click
on auth()
应该有方法将我们带到定义函数的 helpers.php
文件。
我们看到一小段带有 if
语句的代码,但无论哪种方式 returns 都是相同的 class,即 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);
}
瞧!
备注
只有当该方法确实存在时才应该这样做,否则你就是在给自己钉棺材。