lundi 23 juillet 2018

Laravel 5.6 - Route api resources 404 error

Looking in-depth in the official docs, I read about using apiResources to be more precise in routes definition for API controllers - especially if they don't include any HTML. That's my case.

I'm on a Laravel 5.6 project that uses AJAX to fetch data inside Vue components. Until today, I was using normal controllers - placed in /app/Http/Controllers and with Route::resources declared in /routes/web.php.

It were working fine until today, when I tried to refactor them as the docs sugests, like this:

/app/Http/Controllers/API/ItemController.php

<?php

// Definizione Namespace
// before: App\Http\Controllers;
namespace App\Http\Controllers\API;

use App\Item;
use Illuminate\Http\Request;
// Added after refactoring
use App\Http\Controllers\Controller;
use App\Http\Resources\Item as ItemResource;

class ItemController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return ItemResource::collection(Item::all());
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $item = new Item();

        $item->codice = $request->codice;
        $item->data_acquisto = $request->data_acquisto;
        $item->serial = $request->serial;
        $item->labeled = $request->labeled;
        $item->estensione_garanzia = $request->estensione_garanzia;
        $item->stato = $request->stato;
        $item->data_dismissione = $request->data_dismissione;
        $item->codice = $request->codice;

        $item->save();

        return response()->json([
            'success' => 'Item salvato'
        ]);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Item  $item
     * @return \Illuminate\Http\Response
     */
    public function show(Item $item)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Item  $item
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Item $item)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Item  $item
     * @return \Illuminate\Http\Response
     */
    public function destroy(Item $item)
    {
        //
    }
}

/routes/api.php

// Default stuff
[...]

// Resources
Route::apiResources([
    'componente' => 'ComponenteController',
    'condizione' => 'CondizioneController',
    'fornitore' => 'FornitoreController',
    'gruppo' => 'GruppoController',
    'gruppoPermesso' => 'GruppoPermessoController',
    'componente' => 'ComponenteController',
    'item' => 'ItemController',
    'locazione' => 'LocazioneController',
    'permesso' => 'PermessoController',
    'tipologia' => 'TipologiaController',
    'utente' => 'UtenteController'
]);

Now on load, I'm getting an exception without error message, thrown by this:

Symfony\Component\HttpKernel\Exception\NotFoundHttpException

Here's an extract of it:

enter image description here

I'm pretty new with Laravel. This is the very first project. Maybe I'm missing something?

Thanks everyone in advance for help.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire