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

成功登录后会话被销毁,或者守卫出现错误,无法保留会话。当在仪表板视图上询问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');
        });
    });
});

中间件:

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',
    ],
],

帐户控制器:

class AccountsController extends Controller
{

    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}}

【问题讨论】:

  • 您将索引定义为public function index(int $id),期待$id。你的路线是Route::get('accounts/', 'AccountsController@index'),没有id。他们需要匹配。
  • 嗨@aynber,ID应该来自Session。
  • 那么你需要把它从索引定义中取出,然后从函数内部的会话中获取。
  • 你能详细说明并展示一些代码吗?
  • 你得到的错误是非常自我解释的。

标签: php laravel session eloquent


【解决方案1】:

您的控制器需要一个 ID,但您的路由没有任何参数。

正如您在 cmets 中指出的那样,您希望从会话中获取 ID。

您可以使用$value = session('key');从会话中获得一些东西

所以你的控制器应该是这样的:

public function index()
{
    $id = session('nameOfSessionKey');
    $admin = $this->adminRepo->findAdminById($id);
    $talentRepo = new AdminRepository($admin);

    return view('admin.accounts');
}

阅读更多关于 Laravel 中会话的信息documentation

【讨论】:

  • 这是主要问题,我无法使用会话密钥。我在上一个文件的accounts.blade中已经提到了。
  • 您的密钥名为“YOUR_SESSION_KEY”?
  • 是的,请在 accounts.blade 中查看: if (Session::has('YOUR_SESSION_KEY')) {{-- do something with session key --}} else {{-- session key does not exist --}} //this has been print is the id variable is not passed endif
猜你喜欢
  • 2020-05-07
  • 2019-10-29
  • 2020-04-22
  • 2019-07-06
  • 2020-12-11
  • 2020-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多