0
votes

I have a problem I can't figure out. I wrote the back-end admin section with Laravel and Blade and added API routes for the front-end (The Angular App). I am using the yeoman angular generator, then I build the front-end project and move all the folders in "dist" into Laravel's public folder; and the index.html into /app/view/ and rename it to index.php. All loads fine but when I do an angular $http request, the response header says 200 Ok but there's no data returned. What am I missing?

/* routes.php ------------ ANGULAR.JS API ROUTES --------------- */
Route::group(['prefix'=>'client_api'], function()
{
    Route::get('all-from-species', 'ClientApiController@AllFromSpecies');
    Route::get('{animal}', 'ClientApiController@AnimalData');
    Route::get('events', 'ClientApiController@AllEvents');
    Route::post('subscribe', 'ClientApiController@subscribeToNewsletters');
    Route::get('aboutus', 'ClientApiController@aboutUs');
    Route::get('contactus', 'ClientApiController@contactUs');
});

ClientApiController.php -------------------------------

<?php
class ClientApiController extends \BaseController {

    public function AllFromSpecies()
    {
        //
    }

    public function AnimalData($id)
    {
        //
    }

    public function AllEvents()
    {
        //
    }

    public function subscribeToNewsletters()
    {
        //
    }

    /**
    * @return Response::json
    */
    public function aboutUs()
    {
        $about = AboutUs::find(1);
        // Return Json for Angular use.
        return Response::json($about);
    }
}

Angular JS file ----------------------------

angular.module('animalShelterApp')
  .controller('AboutCtrl', function ($scope, $http) {
        $http.get('/client_api/aboutus')
            .then(function(response) {
                $scope.aboutus = response;
        });
  });
1

1 Answers

0
votes

I don't know if this is the only issue, but there's surely a problem with your routes...

You have this route Route::get('{animal}', 'ClientApiController@AnimalData');

It will basically catch every request with client_api/anything. It will also run when you make a request for client_api/aboutus.

You can change that by putting it at the very end of your route group:

Route::group(['prefix'=>'client_api'], function()
{
    Route::get('all-from-species', 'ClientApiController@AllFromSpecies');
    Route::get('events', 'ClientApiController@AllEvents');
    Route::post('subscribe', 'ClientApiController@subscribeToNewsletters');
    Route::get('aboutus', 'ClientApiController@aboutUs');
    Route::get('contactus', 'ClientApiController@contactUs');
    Route::get('{animal}', 'ClientApiController@AnimalData');
});

That means only if none of the above routes match, {animal} will run.