How do I use Laravel with AngularJS? - php

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.

Related

Can i receive data from a database with Angular?

I start a new project with the framework Angular, to try and learn how to use this framework,
in my project i want to do a list of people, with name, age, mail ... with informations from a database but i don't know if it's possible or not (if you have a tutorial ;) )
You want to use a Node.js backend and then use Express.js
The express backend will provide routes to which you can make HTTP requests from Angular.
The code within the routes will query your database and return the result in an HTTP response back to Angular like this:
Angular --- (HTTP request) ---> Node.js Express server --- (query DB) ---> DB
And reverse the arrows for the HTTP response.
This is industry standard.

How To integrate Angular 4 With Php frontend (Existing Project) for page Rendering and backend Nodejs

i have to integrate Angular 4 in my existing Php(Codeigniter) Project but i am not able to find right way to integrate angular 4 . any one know this process.please Share
If you are using node.js for backend. You wont need Php. It will be just NodeJs and Angular.
Without NodeJS, You can use Php to write your APIs and have front-end in Angular which will call those APIs using HttpClinetModule.
If you want to use Angular4 you should remake the frontend in Angular4.
Then on your NodeJS backend create rest routeshttp://localhost:3000/tasks for example would return a JSON object.
Then connect to your NodeJS route (http://localhost:3000/tasks) (from angular) using HttpClientModule and it'll return a JSON object then return that data from your service to a component that you want to display the data in, and either subscribe to it in the component or use the async pipe to unpack the data.
P.S you could also use your current PHP project and make that into an API.

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.

Need Help About Laravel Api

I have two web Application that's work in different domaine , The first one is a Laravel web application like this
domaine1.com
and another web application that's built also with laravel
domaine2.com
and i have on the first application a dashboard like this ( domaine1.com/dashboard) ,
I want to add from this dashboard for example A books to the the web Application 2 , that's have it own database and tables ......
Please can someone tell me how can i do it ?
i'm kind of Newbie on laravel , i think i should Use something like API ? or Something else ?
Create a route in your Application 2 routes.php to get the books
for example
Route::get('domaine2.com/books', 'BookController#getBooks');
In your BookController:
public function getBooks()
{
$books = Books::all(); //I assume that Books is your model
return $books
}
Now all you have to do is call this in a function in your controller from domaine1.com application to have your books
$books = file_get_contents('http://domaine2.com/books');
To give it a try first and be sure it works try accessing http://domaine2.com/books to see if you get a json with your books.
Yes. You should use API. Although it will not be that easy, because you want to create cross-domain requests.
If data you want to pass is not classified, you can make a public API handler, meaning that everyone will be allowed to access this. This can be for example a get request that return some objects from your database. Let's say you create a route in your domain1.com:
GET domain1.com/api/books that returns json/data
Then if anyone visits http://domain1.com/api/books he will see this response formatted in json. You can utilize it in your domain2.com app using CURL or built in vue.js with axios.
If data you want to provide is classified or you want to make requests other than GET (POST for example) you will have to read about application authorization. Putting in a simple way: you will have to show your domain1.com app that someone who wants to access restricted data is allowed to do it.
By the way, mentioning other response: you should utilize api.php, not web.php in routes. And have in mind that file_get_contents is significantly slower than CURL.

Laravel 5 website and API using the same app

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.

Categories