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.
Related
I've developped an simple FileUploader Service based on the Symfony documentation found here: https://symfony.com/doc/current/controller/upload_file.html
My app is just a simple API called inside a Ionic Mobile App.
What I want to know is what is the best practices to give access to my uploaded images.
For exemple, I have an Sport entity App\Entity\Sport that stores an image:
When i request GET /api/sports/ I want to send as response the full url to the image so it can be displayed inside the mobile app (e.g. http://symfony/api/public/uploads/sports/XXXXX.jpg)
Maybe I'm overcomplicating the issue but do I need to make an GET /public/uploads/sports/{id} as an endpoint to get my pictures ? because if possible i would like to have the pictures instantly available after the GET /api/sports and not having to query another time for the picture aswell
Why not just simply add the URL into a field of the JSON response object, where you can access the the picture directly? IMHO there is nothing wrong with that.
I find https://github.com/WhiteHouse/api-standards a good reference to start with if it comes to these kind of decisions.
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.
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.
Details
I'm new to RESTful API & Laravel world.
But "Every artist was first an amateur. - right ? "
Let's get to the point.
I have 2 sites. Let's call it :
A
B
Website B have a a nice list if users table + all of its relation.
I want to allow website A to access website B and load those nice data.
I never done this. I assume that the logics are :
Website A will need some kind of api_keys to access into website B.
Then after the api_key match, website B will return the data back to website A as json file.
Then, website A will receive that json file, and load them into HTML and display it.
Is my logic is even close ? Please - correct me if I am wrong. :)
Here is what I've tried
After doing some researches, I came across this site. I really liked it. I finished it all way.
Now, I kind of get a sense of RESTful API a little more.
Then, I came across this site. I found this
filters.php
Route::filter('api', function() {
// Fetch a user record based on api key
$user = User::where('api_key', '=', Input::get('api_key'))
->take(1)
->get();
if ($user->count() > 0) {
Auth::onceUsingId($user[0]->id); // Authorize the user for this one request
} else {
return Response::view('errors.404', array(), 404)->header('Content-Type', 'application/json');
}
});
I notice the OP of this, stored api_key in users table.
My questions
Do I need to do that like him ?
Is this the only way to do it ?
Is there a better/easier way to do this ?
Rather than grab the api_key from the database, Can I just manually set it to a random number + text like this '21sdf364rt7y6r5ty1u28x1h8gt7yt2ert3654871' ?
How long will the api_key be expire ? Is it even expire ? How do we know that ?
Again, my main goal is to allow website A to access the stuffs from website B.
Is my logic is even close ?
Yes it close enough.
So, the main goal is to access the stuffs from website B. You decide whether stuff from B is restricted content or not.
If users that can access the stuff is limited, yes you need an api keys.
If content for site B is for public, you can just print json data without any required api keys like github does: https://api.github.com/users/github.
good day,
I am using Laravel to build both website and APIs for mobile backend, my API controller is setup like this:
Route::controller("api/", "ApiController"); //mobile APIs, response is in JSON
and other routes and like this:
Route::controller("/", "MainController");
I need to setup a subdomain for the APIs, for instance:
http://api.mysite.com
to route directly to API controller, I need that because I don't want the URL to get long and ugly. Any suggestions ?
Try using this
Route::group(array('domain' => 'api.mysite.com'), 'ApiController');
You can use route grouping for this. Just point the subdomain to the same location as the normal domain and add a route group.
More information is available in the docs.