Routing with AngularJS and Slim PHP

后端 未结 2 1866
情书的邮戳
情书的邮戳 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 :)

提交回复
热议问题