Route::post('/this', 'TheController@post');
public class TheController extends Controller
{
public function post(Request $request)
{
}
}
Hello all, Im a beginer on laravel and i wanted to know how could i do some post route. Thanks you
Route::post('/this', 'TheController@post');
public class TheController extends Controller
{
public function post(Request $request)
{
}
}
thank for answer, my problem is about api, i don't know how past var through my app to my api.
If you're using axios then just use the post method.
axios-.post('/your-route', {'this': 'great', 'that': 'awesome'})
.then(response => thing = response.data)
@antoine_herbert that's totally different question..how do you send your request? using HTML Form? JavaScript? Postman? cURL? Any other mobile/desktop app?
I'm trying to post from form in my app, my form :
@if(!empty($_GET['code']))
<form method="POST" action="/modifications/envoie">
@csrf
<input type="hidden" name="code" id="code" value="{{$_GET['code']}}">
<input id="test" name="test" type="text" placeholder="test">
<button type="submit">Test</button>
</form>
@endif
Here's the route :
Route::post('/modifications/envoie',function(){
ModificationsController::Post($_POST);
return redirect()->back();
});
The controller method :
public static function post($post){
$url = env('API').'/api/produit/'.Input::get('code');
$data = 'code='.$post['code'].'&test='.Input::get('test');
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
}
the API's route :
Route::post('/produit/add/one',function(){
Produit::addOne($_POST);
});
and the API's method :
public static function addOne($post){
return DB::connection('db')->update('UPDATE table_produit SET COLORIS = "'.$post['test'].'" WHERE code='.$post['code']);
}
There are some issues in your code. Especially the way you defined your form action, that is not the correct way. Normally the laravel api is prefixed by api/ that you have missed in your action.
So, I recommend you to check this entry-level tutorial to understand how to use laravel and vue JS together.
https://www.itsolutionstuff.com/post/laravel-vue-js-axios-post-request-example-and-demoexample.html
@antoine_herbert Good, you are using CURL, here is the doc from php.net - https://www.php.net/manual/en/function.curl-setopt.php
// took from examples
$data = array('name' => 'Foo', 'file' => '@/home/user/test.png');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
You might be following some unnecessary steps, to be honest. I am not sure what exactly is your business logic, but you may reduce some steps.
Calling the api endpoint directly is more meaningful to me, isn't that?
That's why I suggest you check the link. You may google it, you will find more tutorials about it. Check whichever you like.
@tisuchi Your link talk about Vue Js and axios post but im actuelly using php curl to call my API
I am sorry about it.
But you will get many tutorials about curl. Check these to get the idea.
Please or to participate in this conversation.