Routing with AngularJS and Slim PHP

一世执手 提交于 2019-12-01 00:28:16

I'm not using php but rather NodeJs. However, this is what I've noticed when using routing with AngularJs and the backend.

The Initial Request

When a user will makes an initial request for your app. It goes through the php logic first. (e.g. $app->get('/requests', 'getRequests')). In my case The job of the php/back-end here is two things:

  • Get data from back-end for SEO purposes-only (most crawlers don't execute client-js so you need to insert that data before you send the page to the user)

  • Most Importantly, give the index file on your angular app along with all the JS. Once the user receives that, Angular bootstraps and you're good to go.

Subsequent Requests

Once the user has loaded your Angular app. The server (php) knows nothing about how the user navigates within your angular app. Remember, angular is client-side and tries to reduce the number of request to the server. When the user navigates to "(#)/requests/1234" it will fire the .when('/requests/:id' route but not the $app->get('/requests/:id', 'getRequest');. If you want to access an endpoint that gets data from your db, you need to use the $http service within angular and do something like this $http.get('requests/1234') and get the data that way.

Let me know if this wasn't clear, upvote/accept if it was :)

Hey everyone thanks for the input I was able to diagnose the issue!

using the network tab on chrome developer tools I saw that my code was sending the word "id" and not a variable.

I have now modified my controller by simply adding my route parameters onto the resource string. Seems to be working great now! Thank you everyone

app.js

app.controller('viewRequestController', function($scope, $location, $route, $routeParams, $resource) {
        $scope.message = 'Pow!';
        $scope.header = 'View Change Request';

        var request_Id = $routeParams.id;
        var Request = $resource(('http://pdgrosit02v/changeRequest/app/api/requests/'+ request_Id));

        $scope.request = Request.get();
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!