How to connect Laravel 5.4 backend with Angular 5 Frontend - javascript

I am working on a project where I need to develop frontend in angular 5 and backend business logic in laravel 5.4 with mySql Database. I am a newbie to this technology and dont know how to develop data flow connection between these two framworks.
It will be helpful if you tell me the easiest and most generic way to solve this out.

As with any other technology, you'll need to work with an API.
In your Laravel project:
Your Laravel project should have models, controllers and a file for the route paths for each method in your controllers, right? If you don't know where the routes are, there's a dedicated folder called "routes" at the root of your project.
Inside this folder, you'll have two important files: api.php and web.php. These two work exactly the same way, but the api.php will automatically add '/api' to the beginning of your route.
A route looks like a link, such as: "yourdomain.com/api/person/name/jondoe". Every route has to be accessed through an HTTP method, usually GET, POST, PUT or DELETE.
Example: let's say you need your user to see a list of people. You'll probably have a Person model and a PersonController containing some function called displayAllPeople(), and this function will select the people from your database. In your api.php file you'll add something like:
Route::get('/people', 'PersonController#displayAllPeople');
You can test that in your browser. Try to access http://yourdomain.com/api/people. You should see a JSON output of your function's return data. In this example, a list of people. Now all we have to do is make your Angular project call this API route.
Source: https://laravel.com/docs/5.7/controllers
In your Angular project:
Remember that Person model you created on your backend? Angular will access that model too, so you'll have to create a Person.ts model here. While that's not required to work, it's a good practice to create this model with the same attributes as the backend model.
Now you'll need a service. The service is a file that will access that Laravel route for you. If you're using Angular CLI, just run the command:
ng generate service person
That will create a person.service.ts file for you. Inside this file, you'll write all the functions to select people, update, save, delete people, and so on. Your function will look something like this:
findAll(): Observable<Person[]> {
return this.http.get<Person[]>('yourdomain.com/api/people');
}
Whenever you call the findAll() function, you'll access the Laravel route, which will return the People from your database. You can do that with any operation you want. Just make sure you follow the HTTP standards.
That also works if you want to send some data from your frontend to the backend, like if you want to save some new Person in your database. You'll need a POST call sending your new Person model which will be received by your Laravel's Controller function, and then persist it in the database.
Source: https://angular.io/tutorial/toh-pt4

Related

How to connect laravel api with pure html website

Since I am new to laravel api, I don't know hot to connect laravel api to html endpoint. My laravel api is working well and html web pages also completely finish. I just want to connect them together... Please explain how to connect these two.
Thank you
I would suggest two ways to go about this but it all depends on what your API does. If you are looking to serve the HTML with Laravel and have some parts of the application loaded by Laravels view() method, you'd basically need to break your HTML into blade files in resources/view folder and call the blade files via view() in controller to load the desired page.
However if you are looking for a separation of view and API where API is called by the view only for some information, you'd need to utilize AJAX via JavaScript to make a call to the API endpoint and retrieve the data (JSON) for use in your HTML site.
I use axios a lot and here is a sample call:
axios.get(url).then(response => { // do whatever here with the response data });
I'm not sure I understand you. But it reads like you developed your Laravel Application (PHP) and HTML separately. Laravel uses Blade (see: https://laravel.com/docs/7.x/blade) as a template engine into which you can inject PHP objects. Basically, the call of a web page (more or less) works like this:
The user is calling the url
URL goes to the routes/web.php
in this file you can call e.g. a controller
the controller called via (e.g.)
return view('my.site.nice-site', [ key => value]);
blade starts and displays the page to the user with the given key as variable.
I hope this helps you a bit. Otherwise I recommend, just to get started, the documentation from Laravel or YouTube.

How to pass data from one Javascript controller file to another?

So, the situation I have gotten myself seems to be a little complicated but I'm hoping there is a simple solution to it.
I have two Javascript controller files, one called rootPageClient.js and another called findPollClient.js. There is also a file called ajax-functions.js which basically contains two functions to route the ajax calls to the right route. The routes and the get and post method functions are contained in the index.js file. The file structure is shown below
Root
Common
ajax-functions.js
Controllers
findPollClient.js
rootPageClient.js
routes
index.js
I have a specific piece of data in the rootPageClient.js file that I need passed through to the findPollClient.js file. All calls are routed through the ajax-functions.js file to the requested route in index.js.
I have figured out that I need to call the same route in the index.js file from both controller files in the order that I want to pass the files from and to. But, I'm not quite sure how to do this. Is there a simple way to do this?
Create a service and save the data inside the service. Have the service injected in both the controllers. Whenever you change some data in service in one of the controllers, it will retain when you look it up in other controller. This is because services and factories are singletons, so they are the right place to store application state and access it anywhere you want.

Web API - Controller Design

I have a Web API project that currently has a ServicesController. I get the list of services, for a server, by making this call:
$http.get(rootWebApiUrl + '/api/services/' + server)
The ServicesController has this signature:
public IEnumerable<FirstSolarService> Get(string id)
Now I want to make two more calls.
Get the Windows folders within the path for the service itself.
After the user selects a folder in #1, show the DLLs in that folder.
I have a choice to make as far as my Web API controllers go. Do I put this all in the ServicesController, or should I create separate controllers for each type of object I'm returning? If the latter, then I would create these two controllers:
FoldersController
FilesController
But what's awkward about that (maybe) is that I'd call each of those by passing in something other than the ID of the folder or file. To get folders, I'd pass in the service name. To get files, I'd pass in the path of the service. Is that the way it is supposed to be done? I'd just like to do this the correct way.
I don't think you should split it up into multiple controllers, unless they each are going to each have considerable functionality, or if Folders and Files are real domain objects/repository entities.
As you may know, you are not limited to the Get/Post/Put/Delete convention for method names in WebAPIs. You can specify the action name if you want. For example, if you wanted a Folders method, you could add one:
[HttpGet]
public ReturnType Folders(string serviceName)
{
}
Your API URL for the above would be '/api/services/folders/' + server.
You don't need to create a custom route -- the default one incorporates an {action} component. Philosophically, I would ask yourself: are services composed of folders? Can folders exists without services? If folders depend on services, then I think it's ok to include that functionality in the services controller. If they don't, then split up the controllers based on functionality.

Custom request urls in Ember model

I am trying to use Ember data with already built REST api. It works fine for top level routes, for example I have route for courses on api side like following:
app.get('/courses', app.controllers.courses.findAll)
app.get('/courses/:id', app.controllers.courses.findById)
Above works fine and I can easily get data with Ember Data. But problem arises with routes that have multiple levels. For example, courses can have multiple topics and topics can have multiple lectures so routes for them are defined like this on api side.
Topics:
app.get('/courses/:course_id/topics', app.controllers.topics.findAll)
app.get('/courses/:course_id/topics/:id', app.controllers.topics.findById)
Lectures:
app.get('/courses/:course_id/topics/:topic_id/lectures', app.controllers.lectures.findAll)
app.get('/courses/:course_id/topics/:topic_id/lectures/:id', app.controllers.lectures.findById)
So if I want to get all lectures inside a course I need to specify course id and topic id as well (not in the query but in url body, as you can see from url structures in backend api).
In Ember I have models for course, topic and lecture but I dont know how can I specify custom urls so that Ember Data can use those urls when I make requests.
One way could be to manually make ajax requests but this way records will not be populated in Ember Data Store.
Or I could have defined relationships between models in Ember but that would require changes on backend api also which is not an option for me.
So is there any nice way to solve this problem?
I am using:
Ember: v1.6.0-beta.2
Ember-Data: v1.0.0-beta.7
Avoid Ember Data, Ember works just fine without it.
If you really want to use it, make ajax calls then use store.pushPayload('type', data) to sideload the data into the store if you really want it in the store.

Laravel + AngularJS application structure

Currently im in the process of architecturing an application based on Laravel-4 and AngularJS
I do not want to create a single page application.
Is it ok to structure the application along the follwoing lines:
EventsController
Get() returns a laravel view
index() returns json array of events
show() returns json event object
Store() saves a json event object
and generally all the controllers will act in this way, having a get method that serves a laravel blade view and is then manipulated via AngularJS and its partials. Is this best practice?
The plan
Use laravel to create the routes and let laravel create the views. You only need to create different view templates, which hold references to your angular controllers.
PagesController
What you could do is create routes for pages, which renders the views.
So you could have a PagesController which can do this
EventsController
Then use angular to call for the data and have a EventsController for that.
In the EventsController you could add your CRUD, which calls a Event model.
Then this will be the way the traffic get's called
The Model needs to return data.
The Controller then will return json, with http status codes.
Mini example
Underneath is a way how I do this with flash messages when logging in.
return Response::json(array('flash' => 'Invalid username or password'), 500);

Categories

Resources