Routing with AngularJS and Slim PHP

后端 未结 2 1864
情书的邮戳
情书的邮戳 2021-01-06 13:46

I have been working with AngularJS and I am trying to connect my application to it.

So far I have used Slim PHP and can get all records from my MySql database but I

相关标签:
2条回答
  • 2021-01-06 14:02

    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 :)

    0 讨论(0)
  • 2021-01-06 14:16

    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();
    });
    
    0 讨论(0)
提交回复
热议问题