Following is my route file i.e web.php
Route::post('finddomainname','DomainController#finddomainname')->name('finddomainname');
Route::post('registerdomains','DomainController#registerdomains')->name('registerdomains');
Following is the code on my DomainController for both the actions used,
public function finddomainname(Request $request)
{
$this->validate($request,
['searchdomaintxt'=>'required',
'searchdomainext'=>'required']);
$searchdomaintxt = $request->input('searchdomaintxt');
$searchdomainext = $request->input('searchdomainext');
$domainname="";
if($searchdomaintxt && $searchdomainext)
{
foreach($searchdomainext as $ext)
{
$domainname.=$searchdomaintxt.".".$ext.",";
}
//dd($domainnames);
$domainnames= rtrim($domainname,',');
$response=$this->soap->multidomainsearch($domainnames);
$result=$response['RESPONSE']['DOMAINSEARCH'];
//dd($result);
if($result){
//return redirect()->action('searchresults', array('response' => $result));
return view('domain.searchresults',['response'=>$result]);
}
else
{
return view('domain.searchresults',['response'=>'']);
}
}
}
Following is the second action on which control come after submitting data
public function registerdomains(registerDomainsValidation $request)
{
$domains=$request->input('selecteddomains');
$selectedyear =$request->input('selectedyear');
$domaincontactid=\Session::get('domaincontactid');
$alldomains='';
foreach($domains as $domain)
{
$alldomains.=$domain.",";
}
$alldomains=rtrim($alldomains,',');
$response=$this->soap->registerdomains($alldomains,$domaincontactid,$selectedyear);
return view('domain.searchresults',['response'=>$response]);
}
but when i submit data it will show me this error
protected function methodNotAllowed(array $others)
{
throw new MethodNotAllowedHttpException($others);
}
You are trying to access your POST route using a GET request, that's why you are receiving a MethodNotAllowedHttpException. To solve this issue, make sure that your <form></form> tag contains the appropriate method attribute.
<form action="{{ YOUR_URL }}" method="POST">
...
</form>
Or if you want to perform the request inside your controller, you can use Guzzle to send http requests. In your controller you can do:
use GuzzleHttp\Client;
$client = new Client();
$response = $client->request('POST', 'YOUR_URL', [
'form_params' => [
'foo' => 'bar'
]
]);
Related
I will pass a parameter in the request. The query won't change. How can I pass the request to SurveyResouce
public function getAllSurveys(\Illuminate\Http\Request $request) {
$surveys = DB::table('surveys')
->select('id', 'survey_name')
->get();
return response()->json([
'error' => false,
'data' => SurveyResource::collection($surveys)
],
}
I want get the request parameters in the resource
public function toArray($request) {
$controller = new SurveyController(new SurveyRepository());
return [
'question' => $request->has('ques') ? $request->input('ques'):'',
];
}
You can try by directly using request() helper function. Like request('parameter');
I have a problem when updating the URLImg data when I use the PUT method, it throws the following error in Postman 'ErrorException: Array to string conversion in file' but if I use the POST method I have no problem uploading the urls of my images.
public function store(Request $request)
{
$values = $request->except('URLImg');
$data = $request->only('URLImg[]');
if($request->hasFile('URLImg')){
foreach($request->file("URLImg") as $image)
{
$name = Str::random(10).'.'.$image->getClientOriginalExtension();
$path = 'storage/img/';
$image->move($path, $name);
$data[] = $name;
$tramite = Tramite::create($values);
$tramite->URLImg = json_encode($data);
$tramite->save();
}
}else{
$tramite = Tramite::create($values);
$tramite->save();
}
return response()->json($tramite, 201);
public function update(Request $request, Tramite $tramite)
{
$data = $request->only('URLImg[]');
if($request->hasFile('URLImg')){
foreach($request->file("URLImg") as $image)
{
$name = Str::random(10).'.'.$image->getClientOriginalExtension();
$path = 'storage/img/';
$image->move($path, $name);
$data[] = $name;
$tramite->URLImg = json_encode($data);
$tramite->save();
}
}
return response()->json($tramite, 201);
}
Postman Config
Postman Config
Yes, it is almost the same code but I only need to update the URLImg field
Want to use a PUT or PATCH request for form containing file uploads - submit a POST request with method spoofing
<form action="/foo/bar" method="POST">
#method('PUT')
...
</form>
via any javascript framework like vue
let data = new FormData;
data.append("_method", "PUT")
axios.post("some/url", data)
Using _method and setting it to 'PUT' or 'PATCH' will allow to declare route as a PUT route and still use POST request to submit form data
$_FILES will not be populated on a PUT or PATCH request with multipart/form-data - PHP limitation
I need to post data to a payment service(payment.com), and redirect to payment.com(to fill credit card number etc.)
The traditional way to do it is like this:
<form id="form" action="payment.com" method="POST">
</form>
<script type="text/javascript">
document.getElementById('form').submit();
</script>
But I want to do it through a controller, not submit by a form, so no
data could be changed.
This is my controller method, and I've tried two ways:redirect() and Guzzle
I tried to redirect() to the url with post data, but I got "The GET method is not supported for this route. Supported methods: POST."
public function postToPaymentServer(Request $request)
{
$amount=$request['amount'];
$payment=[
'amount'=>$amount,
'auth-id'=>config('auth-id')
];
return redirect(url('api/payment/server'))->with(compact('payment'));
}
ps. Here I made a local route to simulate the payment.com in routes/api.php
Route::post('payment/server','PaymentController#server');
I tried to use Guzzle, but it wouldn't redirect to the post url.
public function postToPaymentServer(Request $request)
{
$amount=$request['amount'];
$payment=[
'amount'=>$amount,
'auth-id'=>config('auth-id')
];
$client = new Client();
$response = $client->post('payment.com',[
'body'=>[
'payment'=>$payment,
'allow_redirects' => true
],
]);
return $response;
}
Any suggestions would be greatly appreciated!
You need to retrieve last location from redirect history and redirect to this location.
Look at the track_redirects option:
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
$httpClient = new Client();
$response = $httpClient->post(
'http://payment.com',
[
RequestOptions::ALLOW_REDIRECTS => [
'max' => 5,
'track_redirects' => true,
],
RequestOptions::FORM_PARAMS => [
'payment' => $payment,
],
]
);
$lastLocation = end($response->getHeaders()['X-Guzzle-Redirect-History']);
return redirect($lastLocation);
Is thier anyway to do somethins like this ,
in web.php
Route::get('/test', 'testController#test');
in test Controller
public function test ($url)
{
//while $url store test in route
}
I know only if I send parameter I have to use
Route::get('/{test}', 'testController#test');
UPDATE
I want to do something like this
Route::get('/test', 'testController#test');
Route::get('/test2', 'testController#test');
in my controller
public function test ($url)
{
while $url store test,test2in route
}
LASTEST UPDATE
I dont want to use {url}
I want to make /test = $url when I enter to url/test
In my web.php I use this
Route::get('/test', 'testController#test');
Route::get('/test2', 'testController#test');
The reason that I want to do something like this because I want to make 1 function that alll route can use In my controller I do this .
public function test($url,$preview=null)
{
//$url shoud be test or test 2
try {
$test = (isset($preview)) ? test::where('test.id',$url)->first()
} catch (\Exception $e) {
return redirect('notfound');
}
}
I dont want todo something like this
Route::get('/test', 'testController#test');
Route::get('/test2', 'testController#test');
and In controller
public function test($preview=null)
{
//$url shoud be test or test 2
try {
$test = (isset($preview)) ? test::where('test.id','test)->first()
} catch (\Exception $e) {
return redirect('notfound');
}
}
You need to combine both elements
Route::get('/test/{url}', 'testController#test');
want to make /test = $url
You can't, but you can have /test?foo=$url instead. So you keep your route like
Route::get('/test', 'testController#test');
Then add Request $request as controller method argument (and you remove $url)
public function test(Request $request) {
...
Finally you obtain your url with
$url = $request->input('foo');
Your Route
Route::post('/test', 'testController#test')->name('test);
If you use blade.
<a href="{{ route('test') }}"
onclick="event.preventDefault();
document.getElementById('test_id').submit();">
Test Click
</a>
{!! Form::open(['url' => route('test'), 'method' => 'post', 'id' => 'test_id']) !!}
<input type="hidden" name="url" value="{{ $url}}">
{!! Form::close() !!}
In your controller.
public function test(Request $request)
{
$data = $request->all();
$url = $data['url'];
//do something with your url...
}
My problem is that the method doesn't return a true result.
I want to test if the email of input exists in my entity or not.
Here is the controller:
public function verificationAction(Request $request)
{
if ($this->container->get('request')->isXmlHttpRequest()) {
$email=$request->request->get('email');
$em=$this->getDoctrine()->getEntityManager();
$resp= $em->getRepository("CMSiteBundle:Prospect")->findBy(array('email'=>$email));
$response =new Response(json_encode($resp));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
}
You could try an old-trick. Since in Symfony Controller Actions, You must return a Response why not fake a DEAD RESPONSE like so:
<?php
class ABCController {
public function verificationAction(Request $request) {
if ($this->container->get('request')->isXmlHttpRequest()) {
$email = $request->request->get('email');
$em = $this->getDoctrine()->getEntityManager();
$resp = $em->getRepository("CMSiteBundle:Prospect")
->findBy(array('email' => $email));
//$response = new Response(json_encode($resp));
//$response->headers->set('Content-Type', 'application/json');
// THE TRICK IS THAT DIE RUNS FIRST
// THUS SENDS YOUR RESPONSE YOU THEREBY
// STOPPING THE RETURN FROM FIRING... ;-)
return die(json_encode($resp));
}
}
}
Perhaps this very Old Trick still works for you... ;-)