Reputation: 2893
I am trying to figure out a problem I have. I have 2 laravel setups on the same server, each are independent sites controlled via vhosts.
SiteA needs to act as an API to SiteB. So within SiteA I set up an API by doing
Route::group(['prefix' => 'api/v1'], function () {
Route::post('postProject', array('as' => 'postProject', 'uses' => 'APIController@postProject'));
});
For now, the controller function is simply this
public function postProject(Project $project)
{
dd($project);
}
So within SiteB I want to call this API. As such, I set up a route which contains the project object (I have tried both get and post)
Route::get('projects/{projects}/postProject', array('as' => 'projects.postProject', 'uses' => 'APIController@postProject'));
And this controller function does the following
public function postProject($project)
{
$client = new GuzzleHttp\Client();
$req = $client->request('POST', 'http://localhost/api/v1/postProject', [
'body' => $project,
'headers' => [
'Content-Type' => 'text/xml',
'Content-Length' => strlen($project),
]
]);
$output = $req->getBody()->getContents();
return $output;
}
I have localhost as an example url so the real url wasnt exposed. The error siteB receives when making the post request is a 404 not found. If on siteA I set up a simple get function that returns something, siteB can execute this API call without any problem. The problem comes about when I am trying to post something from siteB to siteA.
Additionally, when I pass the $project object, is there any way to send over its related models as well? A $project has one to many relationships with a few other models, and ideally I would like to get the related data over.
Upvotes: 1
Views: 151
Reputation: 1687
I think you just need to eager load the relationships
public function postProject()
{
//dd($project);
Project::with('relation1', 'relation2')->find($id);
}
Upvotes: 1