【问题标题】:Getting auth to work in a package让身份验证在包中工作
【发布时间】:2013-01-28 09:50:20
【问题描述】:

我正在尝试按照此处的指南:http://fideloper.com/post/41750468389/laravel-4-uber-quick-start-with-auth-guide 为访问“admin/”的用户设置授权。但是,我试图在一个被证明很棘手的包中做到这一点。

在 workbench/vendor/user/src/Vendor/User/routes.php 我有:

Route::filter('adminAuth', function()    {
    if (Auth::guest()) return Redirect::to('user::login');
});

Route::get('/login', function()    {
    return View::make('user::login');
});

Route::post('/login', function()    {
    Auth::attempt( ['email' => Input::get('email'), 'password' =>       Input::get('password')] );
    return Redirect::to('/');
});

Route::get('admin', array('before' => 'adminAuth', function()    {
    return 'Hello, '.Auth::user()->email.'!';
}));

登录页面按预期显示,当输入用户信息时,我在数据库中植入了以下错误:

FatalErrorException: Error: Class '\User' not found in E:\cms_root\vendor\laravel\framework\src\Illuminate\Auth\EloquentUserProvider.php line 89

在我拥有的应用程序的 auth.php 文件中:

    'model' => 'User',

我猜这是不正确的,但我不确定如何解决我创建的用户模型:workbench/vendor/user/src/Vendor/User/models/User.php。我试过User::UserVendor\User\User 都不管用。

添加:

User.php 文件顶部有<?php namespace Vendor\User;,我在应用根目录和workbench/vendor/user 文件夹中都做了composer dumpautoload。

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    您可以尝试使用 \Vendor\User\User 吗?前面的反斜杠类似于命名空间的绝对路径。

    【讨论】:

    • 嗨,谢谢,我刚刚尝试将其更改为:'model' => '\Vendor\User\User',,但仍然抛出错误:FatalErrorException: Error: Class '\Vendor\User\User' not found in E:\cms_root\vendor\laravel\framework\src\Illuminate\Auth\EloquentUserProvider.php line 89
    • @Al_ 你真的在写'\Vendor\User\User'吗?因为你需要像这样转义\:'\\Vendor\\User\\User'。否则自动加载有问题。
    • 这取决于命名空间,我应该将模型的命名空间设置为<?php namespace Vendor\User\Models;,然后调用\\Vendor\\User\\Models\\User,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2015-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多