Search code examples
laravellaravel-5laravel-routing

Error on finding controller from Laravel API Router


I created a fresh Laravel framework.

I created a controller named PostsController:

<?php

namespace App\Http\Controllers;

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

class PostsController extends Controller
{
    public function index()
    {
        $posts = Post::get();

        return response()->success(compact('posts'));
    }
}

Then I created a route in the file api.php:

Route::get('posts', 'PostsController@index');

I ran the command

$ php artisan serve`

and I tested the URL

localhost:8000/api/posts

This error occurs:

BadMethodCallException
Method Illuminate\Routing\ResponseFactory::success does not exist.

file: vendor/laravel/framework/src/Illuminate/Support/Traits/Macroable.php
line: 100

throw new BadMethodCallException("Method {$class}::{$method} does not exist.");

I can't understand why this happened. Please help me.


Solution

  • There is no success method on the ResponseFactory. You can find the available methods here.