What is better practice for accessing request values in laravel.
For example I have method update() in laravel controller.
I want to get values from request, which is better way:
method(Request $request) {
$request->inputName;
}
or
method() {
request('inputName');
}
Is it better to create request instance as method attribute or to use Laravel helper method request().
Thanks
Both are the same, the first approach might be better in case you create a custom form request where you do the validation of the form. Other than that both provide the same thing.
In your Controller
use Illuminate\Http\Request;
public function update(Request $request)
{
$name = $request->input('name');
}
I think this is best way to use
method(Request $request) {
$request->inputName;
}
Even laravel documentation suggest this.
laravel provides you with methods to get values from the request
now I have request variable is $request, it is instance of Request
The better practice for accessing laravel request variables:
If GET method, you should use $request->get('variable_name')
If POST method, you should use $request->input('variable_name')
if you want check request has variable you can use $request->has('variable_name')
Good luck
Related
May I know how can I make just a single route so I don't have to repeat it? Thanks in advance.
Route::get('/url', 'CtcFormController#index' )->name('CtcForm.ch');
Route::post('/url/submit', 'CtcFormController#submit')->name('CtcForm.submit');
Route::view('/url/submitted', 'form.submit')->name('CtcForm.submit');
Route::get('/url2','CtcFormController#store')->name('CtcForm.eng');
Route::post('/url2/submit', 'CtcFormController#submit')->name('CtcForm.submit');
Route::view('/url2/submitted', 'form.submit')->name('CtcForm.submit');
As per your given example, you want to handle the variable part of the route which is /url/ and /url12/. Yes! you can handle there both different route using a single route in ways:
Use route variable to handle dynamic url values i.e. url, url2,url3...url12 and so on.
Route::get('/{url}', 'CtcFormController#index' )->name('CtcForm.ch');
Route::post('/{url}/submit', 'CtcFormController#submit')->name('CtcForm.submit');
Route::view('/{url}/submitted', 'form.submit')->name('CtcForm.submit');
Now in your controller methods handing above routes receive extra parameter $url like:
In controller CtcFormController.php class:
public function index(Request $request, string $url) {
//$url will gives you value from which url request is submitted i.e. url or url12
//method logic goes here ...
}
Similarly, method handling /{url}/submit route will be like:
public function submit(Request $request, string $url) {
//method logic goes here ...
}
Let me know if you have any further query regarding this or you face any issue while implementing it.
I am trying to test a custom email verification code with route model binding, when 2 wildcards are used, laravel always returns a 404.
this is my route in api.php
Route::get('/verify_contact_email/{id}/{hashed_key}', 'CustomEmailVerifyController#verifyContactEmail');
this is the controller with verifyContactEmail
public function verifyContactEmail(UserContactEmailVerify $id, $hashed_key) { return $id; }
when I remove the wildcard {hashed_key} and the $hashed_key, the model shows. I read up on laravel routing documentation, there is no mention of multiple wildcards or passing variable thru URL. Am I doing it wrong? Any help is much appreciated.
You should try this
public function verifyContactEmail($id, $hashed_key) {
$id = UserContactEmailVerify::findOrFail($id);
if(isset($id)){
return $id;
}
}
There is no problem with the route model binding or controller functions. The problem was discovered that routes do not work with hashing or bcrypt, str_random(20) has replaced the verification code instead of bcrypt.
I'm using laravel and integrate the payment gateway.when i send the post request then after success or cancellation ,it redirect the referece id like this
http://localhost/multi_vendor/user/paypal/return?flwref=FLW-MOCK-d4f7572650fbe61ecff7fb17a7129859&txref=rave-bxw7c98dymo8so0kwosco0wwscs8ogc
so how can tackle it in laravel?
I made route for this
Route::get('/paypal/return', 'User\PaypalController#payreturn')->name('user.payment.return');
You can create a route as you write above:-
Route::get('/paypal/return', 'User\PaypalController#payreturn')->name('user.payment.return');
Then in User\PaypalController Controller method payreturn, you can do as following:-
<?php
namespace App\Http\Controllers\User;
use Illuminate\Http\Request;
class PaypalController extends Controller
{
public function payreturn(Request $request)
{
//Here you can get the response request values
$ref = $request->flwref;
$TxRef = $request->txref;
}
}
I also didn't understand well your question, but let me try.
Possible appointments:
if your route is like localhost/multi_vendor/user/paypal/return, you must represent all steps in route file, something like Route::get('/multi_vendor/{user}/paypal/return,[...]). If you are already using somethink like it or a group with prefix, ignore it;
if you wish to redirect, you could use return redirect('yourCompleteUrl') in your controller or in route file;
if you wish to retrieave the parameters Tim Lewis anserwed what you need:
public function payreturn(Request $request)
{
$flref = $request->input('flref');
$txref = $request->input('txref');
}
#darnish manzoor just add your url callback rootverto verifycrsf token
if your redirection url is /ravecallback, just add it and it will stop giving you method not allowed
protected $except = [
'/ravecallback'
];
I have a controller like this.
public function barcode_approve(Request $request)
{
$barcodes = $request->input('barcode_values');
$upload_ids = $request->input('upload_id');
....
}
It is defined as post in route without any problem.
Route::post('my/url','BarcodeScanController#barcode_approve');
I would like to use barcode_approve by sending request from controller like
public function push_approve(){
$request = ['barcode_values' => '23ssdwe','upload_id'=>234234];
$this->barcode_approve($request);
.....
}
But $reqeust->input doesn't give the value when I send it through controller function (push_approve)
How can I send values from another controller function as input in $request?
Your barcode_approve expects a Request object, not an array. While you could instantiate a new Request object and potentially inject your fake inputs, I would consider that a bad practice.
Whenever you find yourself trying to call a controller method from another controller method, it usually means you have logic that can be extracted, either to a model, a trait, or in the case of the same controller, a separate protected function.
The purpose of a controller is to be the transport method, you shouldn't have much business logic in it, extract logic to the models when you can.
Required argument in barcode_approve() method must be instance of Request, not just array even if u call it $request.
So if you realy need your code to work, you must create Request instance at your push_approve method()
$request = new Request(['barcode_values' => '23ssdwe','upload_id'=>234234]);
But better way will be to route Request to push_approve() initially
extends your controller with the controller you want to used
then call the function this->barcode_approve($request)
I'm trying to pull two parameters into my Controller's __construct() method, but I keep getting the following debug
Call to undefined method Illuminate\Routing\Router::parameters()
What I'm specifically trying to do is.
Pull the "portal" and "issue" parameters into my __construct and set the $currentPortal and $currentIssue variables in my class (protected variables)
Use those two protected vars when I please in different Controller actions
Down below you'll find my __construct() method
protected $portals, $issues, $currentPortal, $currentIssue;
public function __construct(\App\Entities\Portal $portals, \App\Entities\Issue $issues){
$this->portals = $portals;
$this->issues = $issues;
dd(\Route::parameters());
if($portal = \Route::getParameter('portal'))
$this->currentPortal = $this->portals->findBySlug($portal);
if($issue = \Route::getParameter('issue'))
$this->currentIssue = $this->issues->findByKey($issue);
}
As you can notice, I'm using dd() as a debug helper in order to see if the current Route parameters and being pulled.
There's no issue in the router.php definitions, as I have already tested them before trying to implement this handy "hack" to pull the current objects.
Any help in order to get the current Route params?
Thanks!
Oh yeah, I figured it out just after posting this question!
I'm leaving the answer in case anyone is having the same issue as I was..
You need to pull the current() route before appending any other requests, so in my case, I should've done
Route::current()->getParameter('MY PARAM NAME')
That should be it