Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5.6-路由api资源404错误_Php_Laravel_Laravel 5 - Fatal编程技术网

Php Laravel 5.6-路由api资源404错误

Php Laravel 5.6-路由api资源404错误,php,laravel,laravel-5,Php,Laravel,Laravel 5,通过深入阅读,我了解到如何使用apiResources更精确地定义API控制器的路由-尤其是如果它们不包含任何HTML。那是我的案子 我参与了一个Laravel5.6项目,该项目使用AJAX获取Vue组件内部的数据。直到今天,我一直在使用普通的控制器-放置在/app/Http/controllers中,并在/routes/web.php中声明Route::resources 直到今天,当我试图按照文档的建议重构它们时,它一直工作得很好,如下所示: /app/Http/Controllers/AP

通过深入阅读,我了解到如何使用
apiResources
更精确地定义API控制器的路由-尤其是如果它们不包含任何HTML。那是我的案子

我参与了一个Laravel5.6项目,该项目使用AJAX获取Vue组件内部的数据。直到今天,我一直在使用普通的控制器-放置在
/app/Http/controllers
中,并在
/routes/web.php
中声明
Route::resources

直到今天,当我试图按照文档的建议重构它们时,它一直工作得很好,如下所示:

/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)
    {
        //
    }
}
// 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'
]);
现在加载(在GET/
index()
)时,我得到一个没有错误消息的异常,由以下内容引发:

Symfony\Component\HttpKernel\Exception\NotFoundHttpException

以下是它的摘录:

我对拉威尔很陌生。这是第一个项目。也许我错过了什么


提前感谢大家的帮助。

问题终于解决了

按照@ts8060的建议,我在AJAX调用中向路径添加了默认的
/api
前缀。即:

url:'/api/item'
指向
/app/Http/Controllers/api/ItemController
(本例中指向
index()
方法)

我完全跳过了提到API前缀的段落(需要的人可以找到)

此外,正如Slack社区用户告诉我的(credits:dadibom),我在第17行更改了
/app/Http/Providers/RouteServiceProvider.php
中的路径,以便将调用重定向到正确的目的地(
。/Controllers/API
文件夹):

就这样


再次感谢大家的宝贵帮助。

这只是对您答案的补充,因为我正在寻找解决类似问题的方法。不要在第17行更改$namespace,而是查看mapApiRoutes()并在那里调整名称空间

->namespace($this->namespace . "\API")
这将在API/子目录中查找API.php中路由的控制器


其他路由(即web.php中的)将保留其原始名称空间。这样,您仍然可以使用常规的非api路由

看这一集(免费)谢谢@kyslik的建议,但我没有得到我的具体案例。我认为这可能是由于将控制器移动到API文件夹,但框架找不到它们。请检查此链接以排除路由错误,例如此API路由具有默认的“API”前缀。您是否正在请求使用api/索引创建URL?非常感谢@ts8060,就是这样。之后,我只是在
RouterServiceProvider.php
中更改了路径,以指向正确的路径。请参阅下面我的答案了解详细信息。感谢@paratechx的建议,我将在下一个项目中尝试。
protected $namespace = 'App\Http\Controllers\API';
->namespace($this->namespace . "\API")