【问题标题】:Too few arguments to function App\Http\Controllers\Admin\AccountsController::index(), 0 passed and exactly 1 expected in laravel函数 App\Http\Controllers\Admin\AccountsController::index() 的参数太少,通过了 0 并且在 laravel 中预期正好 1
【发布时间】:2020-05-07 08:45:15
【问题描述】:

成功登录后会话被销毁,或者守卫出现错误,无法保留会话。当在仪表板视图上询问 your_session_key 时,它提供 null。

Route::group(['prefix' => 'admin'], function () {
Route::namespace('Admin')->group(function () {
Route::group(['middleware' => ['admin_middle','auth:admin']] , function () {
        Route::get('accounts/', 'AccountsController@index')->name('admin.accounts');
        });
    });
});

Middleware: App\Http\Middleware\RedirectIfNotAdmin //在内核中注册为'admin_middle' => \App\Http\Middleware\RedirectIfNotAdmin::class,

class RedirectIfNotAdmin
{
public function handle($request, Closure $next, $guard = 'admin')
{
    if (!auth()->guard($guard)->check()) {
        $request->session()->flash('error', 'You must be an Admin to see this page');
        return redirect(route('auth.admin.login'));
    }

    return $next($request);
}
}

Guard: config/auth.php // 自定义 Guard

'guards' => [
'admin' => [
    'driver' => 'session',
    'provider' => 'admin',

],
],

AccountsController:控制器\AccountsController

类 AccountsController 扩展控制器 {

public function __construct(AdminRepositoryInterface $adminRepository) {
    $this->adminRepo = $adminRepository;
}

private $adminRepo;
public function index(int $id)
{
    $admin = $this->adminRepo->findAdminById($id);

    $talentRepo = new AdminRepository($admin);


    return view('admin.accounts');
}

}

AdminRepositoryInterface:App\Shop\Admins\Repositories\Interfaces\AdminRepositoryInterface;

interface AdminRepositoryInterface extends BaseRepositoryInterface
{
public function findAdminById(int $id) : Admin;
}

AdminRepository:App\Shop\Admins\Repositories\AdminRepository

class AdminRepository extends BaseRepository implements AdminRepositoryInterface
{
public function findAdminById(int $id) : Admin
{
    try {
        return $this->findOneOrFail($id);
    } catch (ModelNotFoundException $e) {
        throw new AdminNotFoundException($e);
    }
}
}

查看:admin\accounts.blade

@if (Session::has('YOUR_SESSION_KEY'))
{{-- do something with session key --}} 
@else
{{-- session key does not exist  --}} //this has been printed is the ID variable is not passed
@endif
{{$admin->name}}
 <br />{{$admin->email}}

【问题讨论】:

  • 问题中有很多与问题无关的代码。 @param AdminRepositoryInterface $adminRepository adminRepo 对象是否作为参数传递并被忽略?您希望填充 $this-&gt;adminRepo 什么?
  • 我期待会话的价值。 @AD7six
  • 我想你误解了Naren的问题。
  • 我猜,我有点明白了。我已经更新了一个代码。现在它的不同错误@AD7six
  • 我不知道我觉得警卫出了点问题。 @AD7six 和中间件。请提出一些建议。

标签: php laravel eloquent laravel-5.7


【解决方案1】:

意思是$this-&gt;adminRepo 返回null

尝试在控制器的构造函数中初始化您的$this-&gt;adminRepo。如果您键入提示接口,请确保将其绑定到服务提供者中。

https://laravel.com/docs/5.8/container#binding-basics

【讨论】:

  • $this->app->bind(AdminRepositoryInterface::class, AdminRepository::class);是的,我已经在Providers中绑定了
猜你喜欢
  • 1970-01-01
  • 2019-10-29
  • 2020-04-22
  • 2019-07-06
  • 2020-12-11
  • 2020-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多