嗨,您好,我的Laravel路线返回错误代码404

时间:2019-11-13 10:40:18

标签: php laravel api

    //USerController.php
    <?php

    namespace App\Http\Controllers\User;

    use App\User;
    use Illuminate\Http\Request;
    use App\Http\Controllers\Controller;

    class UserController extends Controller
    {
        /**
         * Display a listing of the resource.
         *
         * @return \Illuminate\Http\Response
         */
        public function index()
        {
           $users  = User::all();
           return response()->json(['data'=>$users],200);
        }



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

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


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

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


    //api.php
        <?php

        use Illuminate\Http\Request;

        /*


  |--------------------------------------------------------------------------
    | API Routes
    |--------------------------------------------------------------------------
    |
    | Here is where you can register API routes for your application. These
    | routes are loaded by the RouteServiceProvider within a group which
    | is assigned the "api" middleware group. Enjoy building your API!
    |

    */
    Route::middleware('auth:api')->get('/user', function (Request $request) {
        return $request->user();
    });

    /*Buyers */
    Route::resource('buyers','Buyer\BuyerController', ['only'=>['index', 'show']]);

    /*Categories */
    Route::resource('categories','Category\CategoryController', ['except'=>['create', 'edit']]);


    /*Product*/
    Route::resource('products','Product\ProductController', ['only'=>['index', 'show']]);

    /*Buyers */
    Route::resource('buyers','Buyer\BuyerController', ['only'=>['index', 'show']]);

    /*Sellers */
    Route::resource('sellers','Seller\SellerController', ['only'=>['index', 'show']]);

    /*Transactions */
    Route::resource('transactions','Transaction\TransactionController', ['only'=>['index', 'show']]);

    /*Users */
    Route::resource('users','User\UserController', ['except'=>['create', 'edit']]);

//routes list
| GET|HEAD  | /                              |                    | Closure                                                      | web          |
|        | GET|HEAD  | api/buyers                     | buyers.index       | App\Http\Controllers\Buyer\BuyerController@index             | api          |
|        | GET|HEAD  | api/buyers/{buyer}             | buyers.show        | App\Http\Controllers\Buyer\BuyerController@show              | api          |
|        | GET|HEAD  | api/categories                 | categories.index   | App\Http\Controllers\Category\CategoryController@index       | api          |
|        | POST      | api/categories                 | categories.store   | App\Http\Controllers\Category\CategoryController@store       | api          |
|        | GET|HEAD  | api/categories/{category}      | categories.show    | App\Http\Controllers\Category\CategoryController@show        | api          |
|        | PUT|PATCH | api/categories/{category}      | categories.update  | App\Http\Controllers\Category\CategoryController@update      | api          |
|        | DELETE    | api/categories/{category}      | categories.destroy | App\Http\Controllers\Category\CategoryController@destroy     | api          |
|        | GET|HEAD  | api/products                   | products.index     | App\Http\Controllers\Product\ProductController@index         | api          |
|        | GET|HEAD  | api/products/{product}         | products.show      | App\Http\Controllers\Product\ProductController@show          | api          |
|        | GET|HEAD  | api/sellers                    | sellers.index      | App\Http\Controllers\Seller\SellerController@index           | api          |
|        | GET|HEAD  | api/sellers/{seller}           | sellers.show       | App\Http\Controllers\Seller\SellerController@show            | api          |
|        | GET|HEAD  | api/transactions               | transactions.index | App\Http\Controllers\Transaction\TransactionController@index | api          |
|        | GET|HEAD  | api/transactions/{transaction} | transactions.show  | App\Http\Controllers\Transaction\TransactionController@show  | api          |
|        | GET|HEAD  | api/user                       |                    | Closure                                                      | api,auth:api |
|        | GET|HEAD  | api/users                      | users.index        | App\Http\Controllers\User\UserController@index               | api          |
|        | POST      | api/users                      | users.store        | App\Http\Controllers\User\UserController@store               | api          |
|        | GET|HEAD  | api/users/{user}               | users.show         | App\Http\Controllers\User\UserController@show                | api          |
|        | PUT|PATCH | api/users/{user}               | users.update       | App\Http\Controllers\User\UserController@update              | api          |
|        | DELETE    | api/users/{user}               | users.destroy      | App\Http\Controllers\User\UserController@destroy             | api  

1 个答案:

答案 0 :(得分:2)

 you should call it 127.0.0.1:8000/api/users