Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Api routes fail to load. 404 not found

For reference I have been working with this tutorial https://scotch.io/tutorials/build-a-time-tracker-with-laravel-5-and-angularjs-part-2.

I wanted to become more familiar with laravel 5 since I had previously only used 4 and found the above tutorial which also mixed in a little angular js. I followed part one and two of the tutorials to the letter and set up a database using mysql and phpmyadmin like directed.

I get to a section about halfway through which sets up a group route with the prefix api to pull seeded data from the database and display it in the view.

// app/Http/routes.php

 ...

// A route group allows us to have a prefix, in this case api
Route::group(array('prefix' => 'api'), function()
{
    Route::resource('time', 'TimeEntriesController');
    Route::resource('users', 'UsersController');
});

After this point I go to the page and the area that was previously rendered with data from a file instead of a database is now blank. If I inspect the element I get "failed to load resource the server responded with a status of 404 (not found)" and it displays my path time-tracker-2/public/api/time.

The routes work with these two controllers to populate the page with userdata from my database

// app/Http/Controllers/TimeEntriesController.php

...

use App\Http\Requests;
use App\Http\Controllers\Controller;    
use App\TimeEntry;

use Illuminate\Support\Facades\Request;

class TimeEntriesController extends Controller {

    // Gets time entries and eager loads their associated users
    public function index()
    {
        $time = TimeEntry::with('user')->get();

        return $time;
    }


// app/Http/Controllers/UsersController.php

...

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;

use Illuminate\Http\Request;

class UsersController extends Controller {

    // Gets all users in the users table and returns them
    public function index()
    {
        $users = User::all();

        return $users;
    }

Again I haven't worked much with Laravel and this is my first time messing with angular so I don't know if I am missing something super obvious or what the deal is. I have checked over all my code and compared it to the sample code and they are identical other than my database information. I have also scrapped the project and started from scratch and still get the same error when I get to this point.

Any sort of direction to look would be greatly appreciated because this error is driving me nuts.

like image 775
JCairoli Avatar asked Sep 09 '15 15:09

JCairoli


1 Answers

Remember to point on the public folder, not on the root folder. That's why your URL is time-tracker-2/public/api/time and not time-tracker-2/api/time. This should fix your 404 error.

like image 147
cre8 Avatar answered Oct 04 '22 21:10

cre8