1

我们正在尝试访问当前登录用户的 id,我们以这种方式修改了 auth.php 文件:

'api' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

在此之后我们还编辑了 Kernel.php 文件:

'api' => [
            'throttle:60,1',
            'bindings',
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

在我们的 Route api.php 我们有这个:

Auth::routes();

Route::middleware('api')->group(function(){

Route::post('/tipo', [App\Http\Controllers\ApiController::class, 'createtipo']);
Route::post('/corso', [App\Http\Controllers\ApiController::class, 'createcorso']);
Route::get('/mostracorsi', [App\Http\Controllers\ApiController::class, 'mostracorsi']);
Route::get('/mostracorso/{idCorso}', [App\Http\Controllers\ApiController::class, 'mostracorso']);
Route::put('/updatecorso/{idCorso}', [App\Http\Controllers\ApiController::class, 'updatecorso']);
Route::delete('/deletecorso/{idCorso}', [App\Http\Controllers\ApiController::class, 'deletecorso']);


Route::post('/iscrizione', [App\Http\Controllers\ApiController::class, 'iscrizione']);
Route::get('/vedicorso/{idCorso}', [App\Http\Controllers\ApiController::class, 'vedicorso']);
Route::delete('/deleteiscrizione/{idUtente}', [App\Http\Controllers\ApiController::class, 'deleteiscrizione']);


Auth::routes();

}));

在我们的控制器中我们有这个:

public function createcorso(Request $request)
{

    $corsoscii = new CorsoScii();
    $admin_id = new AdminController();

    $corsoscii->tipo = $request->input('tipo');
    $corsoscii->nome = $request->input('nome');
    $corsoscii->membriMax = $request->input('membriMax');
    $corsoscii->orario_inizio = $request->input('orario_inizio');
    $corsoscii->orario_fine = $request->input('orario_fine');
    $corsoscii->inizio = $request->input('inizio');
    $corsoscii->fine = $request->input('fine');

    $corsoscii->save();

    $idUtente = Auth::id();
    $idCorso = CorsoScii::latest()->first();
    DB::table('iscrizione')->insert(
        array('idUtente' => $idUtente, 'idCorso' => $idCorso)
    );
    return response()->json($corsoscii);
}

我们之所以选择这种方式,是因为每次我们在控制器上传递当前用户 id 时,它总是为空:

SQLSTATE [23000]:违反完整性约束:1048 列 'idUtente' 不能为空

当我们再次尝试执行 ajax 调用时,它会在浏览器控制台中显示以下错误:

未找到绑定异常目标类

如果可能是有用的信息,我们也有路线列表,我们试图了解这里有什么问题:

+--------+----------+---------------------------------+------------------+------------------------------------------------------------------------+--------------------------------------------------+
| Domain | Method   | URI                             | Name             | Action                                                                 | Middleware                                       |
+--------+----------+---------------------------------+------------------+------------------------------------------------------------------------+--------------------------------------------------+
|        | GET|HEAD | /                               |                  | Closure                                                                | web                                              |
|        | GET|HEAD | admin                           | admin            | App\Http\Controllers\AdminController@index                             | web                                              |
|        |          |                                 |                  |                                                                        | Laratrust\Middleware\LaratrustRole:administrator |
|        | POST     | api/corso                       |                  | App\Http\Controllers\ApiController@createcorso                         | api                                              |
|        | DELETE   | api/deletecorso/{idCorso}       |                  | App\Http\Controllers\ApiController@deletecorso                         | api                                              |
|        | DELETE   | api/deleteiscrizione/{idUtente} |                  | App\Http\Controllers\ApiController@deleteiscrizione                    | api                                              |
|        | POST     | api/iscrizione                  |                  | App\Http\Controllers\ApiController@iscrizione                          | api                                              |
|        | POST     | api/login                       |                  | App\Http\Controllers\Auth\LoginController@login                        | api                                              |
|        |          |                                 |                  |                                                                        | guest                                            |
|        | GET|HEAD | api/login                       | login            | App\Http\Controllers\Auth\LoginController@showLoginForm                | api                                              |
|        |          |                                 |                  |                                                                        | guest                                            |
|        | POST     | api/logout                      | logout           | App\Http\Controllers\Auth\LoginController@logout                       | api                                              |
|        | GET|HEAD | api/mostracorsi                 |                  | App\Http\Controllers\ApiController@mostracorsi                         | api                                              |
|        | GET|HEAD | api/mostracorso/{idCorso}       |                  | App\Http\Controllers\ApiController@mostracorso                         | api                                              |
|        | POST     | api/password/confirm            |                  | App\Http\Controllers\Auth\ConfirmPasswordController@confirm            | api                                              |
|        |          |                                 |                  |                                                                        | auth                                             |
|        | GET|HEAD | api/password/confirm            | password.confirm | App\Http\Controllers\Auth\ConfirmPasswordController@showConfirmForm    | api                                              |
|        |          |                                 |                  |                                                                        | auth                                             |
|        | POST     | api/password/email              | password.email   | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail  | api                                              |
|        | POST     | api/password/reset              | password.update  | App\Http\Controllers\Auth\ResetPasswordController@reset                | api                                              |
|        | GET|HEAD | api/password/reset              | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | api                                              |
|        | GET|HEAD | api/password/reset/{token}      | password.reset   | App\Http\Controllers\Auth\ResetPasswordController@showResetForm        | api                                              |
|        | POST     | api/register                    |                  | App\Http\Controllers\Auth\RegisterController@register                  | api                                              |
|        |          |                                 |                  |                                                                        | guest                                            |
|        | GET|HEAD | api/register                    | register         | App\Http\Controllers\Auth\RegisterController@showRegistrationForm      | api                                              |
|        |          |                                 |                  |                                                                        | guest                                            |
|        | POST     | api/tipo                        |                  | App\Http\Controllers\ApiController@createtipo                          | api                                              |
|        | PUT      | api/updatecorso/{idCorso}       |                  | App\Http\Controllers\ApiController@updatecorso                         | api                                              |
|        | GET|HEAD | api/user                        |                  | Closure                                                                | api                                              |
|        |          |                                 |                  |                                                                        | auth:api                                         |
|        | GET|HEAD | api/vedicorso/{idCorso}         |                  | App\Http\Controllers\ApiController@vedicorso                           | api                                              |
|        | POST     | cancella/{id}                   | cancellautente   | App\Http\Controllers\ClienteController@delete                          | web                                              |
|        |          |                                 |                  |                                                                        | Laratrust\Middleware\LaratrustRole:user          |
|        | GET|HEAD | cliente                         | cliente          | App\Http\Controllers\ClienteController@index                           | web                                              |
|        |          |                                 |                  |                                                                        | Laratrust\Middleware\LaratrustRole:user          |
|        | POST     | login                           |                  | App\Http\Controllers\Auth\LoginController@login                        | web                                              |
|        |          |                                 |                  |                                                                        | guest                                            |
|        | GET|HEAD | login                           | login            | App\Http\Controllers\Auth\LoginController@showLoginForm                | web                                              |
|        |          |                                 |                  |                                                                        | guest                                            |
|        | POST     | logout                          | logout           | App\Http\Controllers\Auth\LoginController@logout                       | web                                              |
|        | GET|HEAD | password/confirm                | password.confirm | App\Http\Controllers\Auth\ConfirmPasswordController@showConfirmForm    | web                                              |
|        |          |                                 |                  |                                                                        | auth                                             |
|        | POST     | password/confirm                |                  | App\Http\Controllers\Auth\ConfirmPasswordController@confirm            | web                                              |
|        |          |                                 |                  |                                                                        | auth                                             |
|        | POST     | password/email                  | password.email   | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail  | web                                              |
|        | GET|HEAD | password/reset                  | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web                                              |
|        | POST     | password/reset                  | password.update  | App\Http\Controllers\Auth\ResetPasswordController@reset                | web                                              |
|        | GET|HEAD | password/reset/{token}          | password.reset   | App\Http\Controllers\Auth\ResetPasswordController@showResetForm        | web                                              |
|        | GET|HEAD | register                        | register         | App\Http\Controllers\Auth\RegisterController@showRegistrationForm      | web                                              |
|        |          |                                 |                  |                                                                        | guest                                            |
|        | POST     | register                        |                  | App\Http\Controllers\Auth\RegisterController@register                  | web                                              |
|        |          |                                 |                  |                                                                        | guest                                            |
+--------+----------+---------------------------------+------------------+------------------------------------------------------------------------+--------------------------------------------------+

提前致谢。

4

2 回答 2

0

你必须auth:api在你的api中间件组中。

    'api' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\Session\Middleware\AuthenticateSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        'throttle:60,1',
        'auth:api',
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],
于 2021-03-25T16:09:42.480 回答
0

只需使用middleware('auth'),因为 api.php 中的每个路由都已分配给middleware('api').

于 2021-03-25T16:29:12.770 回答