Laravel 5 website and API using the same app - php

The application I am migrating into Laravel 5 is a website and its API.
The API is used for the mobile apps and for the website to pull the data.
The website will not be developed to be a Single Page App, because I already have all the views and I'm just migrating the website to Laravel 5.
How can I do the following without duplicating my code?
For instance, allow /products to list all my products using this API endpoint (/api/2.0/products).
The same applies to all other routes

Yes you can. I am currently doing this myself. Writing the base API first and then dogfooding it for my own website. There is a good laravel package called Dingo ( https://github.com/dingo/api ) that can give different output depending on where you call it from.
If you call it simply from api.yourwebsite.com/products you will get JSON. but if you call it internally like API::get('/products') you will get array/object whatever you're returning instead.
So I have a website that is using the API to render itself. This way, you only write the API once, and can use it for your frontend website, mobile, or give it to third party developers too.
I hope this answers your question. If you have any more questions, please let me know. Thanks

You can do it by creating repositories for your code.
Then create separate controllers for both APIs and Website. Through that your code doesn't duplicated and you can access that repository from both the controllers and return response accordingly.

Related

Laravel API with Google Analytics

I made a REST API with Laravel, basically I get they route pass the route to the controller and from the controller I get the data and return a json formatted response, I was wondering If there is any way to add Google Analytics tracking to this.
Thanks.
Your best bet seems to be the Google Analytics Measurement Protocol:
https://developers.google.com/analytics/devguides/collection/protocol/v1/
Also, since Laravel has the ability to run packages, it appears someone has already created an appropriate package for Laravel:
https://github.com/irazasyed/laravel-gamp
Note - I have not tried this package so I can't say whether it will save time over implementing the calls directly according to the GAMP docs.

What is the best way to achive realtime info on my website currently using php yii framework and django-rest for api with mysql

Here i have no idea, what to exactly use to get the real-time information on the web page on my already developed application. The real-time feature will be based on online Auction feature, as a bidder bids low/high amount all the concerned user should reflect the newer price on their window immediately without refreshing the web page.
Please suggest me the best way possible, my application based on:
PHP 5 frontend with yii 1.1.15 framework
Python django-rest framework for the rest api to fetch data from mysql database.
for this i have heard about the node js, but will it be possible using only node js without using mongodb/rethinkdb angular or express js or socket.io.
Why do you need bout Yii and Python? DRF is very easy to use and will provide the backend to provide a rest API and the front end can be something as simple as HTML/CSS/js to fetch data from the REST API. Yii will not add any value to this system.
You need to read up on how an API based system works works (which is what you are trying to do.

How do I use Laravel with AngularJS?

I am trying to use Laravel with AngularJS and want to understand the best way to set up such a project.
Should I (A) have a single domain and consume an API from the Laravel project, or (B) have website.com and api.website.com and consume the API as if it were a 3rd party API?
I can see pros and cons for each, but what I can't get my head around is how routing would work with option A. I assume the initial routing would be via Laravel to display a top level view and then from that point onwards AngularJS would do the routing, but surely AngularJS and its routing are only initialized when the page loads. For example, if a user goes to a subroute without hitting the site root, no route on the Laravel side will exists for that and thus would it not respond with 404/Not Found?
What is the best setup for consuming my Laravel API within AngularJS?
I suggest to separate (your option b).
Front and back-end are totally separated
You can replace one of them without problems in the other
Use middleware, json responses and http status codes
Use a framework for back-end too (for example Laravel or Lumen)
About routes ...
Your back-end has its own routes (endpoints).
Your front-end has its own routes (totally different), but should send GET/POST/PUT etc. requests to the back-end. The back-end returns (json) response, which will be parsed by the front-end.
Develop both separately! So you can use the back-end for third party later.

Web services to interact between Joomla backend and iOS/Android app

We just started to develop an app where we need to get some data from a Joomla backend. (Data would be company profiles. Those companies register on the Joomla webiste and the public information gets displayed in our app).
What is the best way to communicate between the app and the Joomla backend? The information from the database just has to be pulled once at the start of the app and maybe by manual refreshing.
So far I have read about REST API and SOAP as web-services. I played a bit around with examples from the internet but wasn't really successful yet. So could one of you give a simple explanation and a small example for the interaction between Joomla backend and an Android app e.g.
If you are looking for a pre-packaged solution to turn Joomla 3.5/3.6 into a Mobile Back-end as a Service (MBaSS) you may want to explore cAPI Core REST API.
https://extensions.joomla.org/extensions/extension/capi-core-rest-api
Please note that this is a commercial plugin which I developed. If you are OK with that, you may read on here for more information from a previously answered question:
https://stackoverflow.com/a/32706378/5361267
API Documentation can be found here:
http://learn.getcapi.org
Swagger UI Docs:
https://demo1.getcapi.io/api-docs
To answer your specific question about profiles, this extension will allow you to read and update profile data associated with any user. In addition to the built-in profile fields, the profile creation methods allow you to create and populate unique profile keys. While these might not be visible in the joomla interface (because they have to be defined in XML as well), they can be queried / updated remotely as needed.
PUT /user/profile/{id}
https://demo1.getcapi.io/api-docs#!/User/putUserProfileById

Codeigniter calling different API's - where to put the API calls

I am making a system where a users account can pull in details about their Facebook, Twitter and a few other API's.
I have been doing it so it brings in the data into the Controller, but is this the right thing to do? I know that "Codeigniter is great because you can use it any way you want it just depends on how you feel comfortable" etc etc but I find I am repeating myself sometimes if I have to bring in latest Tweets in more than one controller, for example.
Thanks in advance
To use an API you have to download API required files in libraries folder and then load library into the controller.
You are then good to go to make API calls.
Here is a sample tutorial:
http://codezone4.wordpress.com/2013/05/19/codeigniter-facebook-login-tutorial-using-facebook-php-sdk/
Why not create libraries for each, then initialize library to the controllers where you want to use the data.
This is how you create library in CodeIgniter.

Categories