Currently I'm working on a project where I made it so that when a user types a correct password in form field, it will give them the items from the given section.
The main problem i'm having is that to do this I need to capture the request and therefore the route has to be a post method instead of a get as such:
public function index(Request $request)
{
$id = $request->input('id');
$password = $request->input('password');
$result = DB::table('scrumboards')->find($id);
if ($result->key == $password) {
$scrumboard = $result;
$items = DB::table('backlogs')->get();
return view('scrumboard', ['items' => $items, 'scrumboard' => $scrumboard]);
} else {
$scrumboard = $result;
return redirect('home');
}
}
and the route as such:
Route::post('/scrumboard', 'ScrumboardController#index');
By doing this, request errors wont work since It wants to redirect back but can't since this is a post method.
Any way I can avoid this clash?
Routes can have multiple HTTP verbs. Define your route as
Route::match(['get', 'post'], '/scrumboard', 'ScrumboardController#index');
to make it available as GET and POST route.
Related
I'm encountering a problem where a redirect from one route to another is calling the targeted controller method twice. This question addresses a similar issue, but the OP passing a 301 status code was deemed to be the issue in the accepted answer, and I'm not specifying any status code. I'm also using the session state for parameters. The relevant code looks something like this:
public function origin(Request $request) {
// Assume I have set variables $user and $cvId
return redirect()
->action('SampleController#confirmUser')
->with([
'cvId' => $cvId,
'userId' => $user->id,
]);
}
public function confirmUser(Request $request) {
$cvId = session()->get('cvId');
$userId = session()->get('userId');
if (is_null($cvId) || is_null($userId)) {
// This is reached on the second time this is called, as
// the session variables aren't set the second time
return redirect('/home');
}
// We only see the view for fractions of a second before we are redirected home
return view('sample.confirmUser', compact('user', 'cvId'));
}
Any ideas what could be causing this? I don't have any next middleware or any of the other possible causes that are suggested in related questions where controllers are executed twice.
Thanks for any help!
Have you tried passing values in parameters? Try the below code.
public function origin(Request $request) {
// Assume I have set variables $user and $cvId
return redirect()->action(
'SampleController#confirmUser', ['cvId' => $cvId, 'userId'=>$user->id]
);
}
public function confirmUser(Request $request) {
$cvId = $request->cvId;
$userId = $request->userId;
if (is_null($cvId) || is_null($userId)) {
// This is reached on the second time this is called, as
// the session variables aren't set the second time
return redirect('/home');
}
// We only see the view for fractions of a second before we are redirected home
return view('sample.confirmUser', compact('user', 'cvId'));
}
For example, I have this route in my web.php :-
Route::get('products/{product}/owners', 'ProductController#showOwners');
When I try to add new 'Owner' to the product, I have to do it in the parent URL, like this :-
Route::post('products/storeOwner', 'ProductController#storeOwner');
And then I pass the product ID in a hidden field in the form, because the post request doesn't accept URL parameters. So is there anyway to do it like below ?
Route::post('products/{product}/storeOwner', 'ProductController#storeOwner');
So the POST request will be sent inside the particular 'product' URL?
UPDATE
/* ProductController Class */
public function storeOwner (AddProductOwner $request)
{
$product= Product::find($request->product);
$user = Auth::user();
if ( $user->ownerOf($product)) {
// Check if the current user is already one of the owners).
// If the current user is the owner then return to the product
// This line is not executed because in (products/show.blade.php) we have set a condition.
return redirect('products/' . $request->product);
}
$join = new Join;
$join->role = $request->join_role;
$join->product()->associate($request->product);
$join->user()->associate(Auth::user());
$join->message = $request->message;
$join->save();
// TODO: we have to make this with ajax instead of normal form
return redirect('products/'. $request->product);
}
I hope my question is clear enough..
Yes you can do as you mentioned in your last route
Route::post('products/{product}/storeOwner', 'ProductController#storeOwner');
And then get the product Id in your functions argument
public function storeOwner (AddProductOwner $request, $productId)
{
dd($productId); // TRY THIS OUT. CHECK THE 2nd ARGUMENT I SET.
$product= Product::find($productId); // PASS THE VERIABLE HERE.
$user = Auth::user();
if ( $user->ownerOf($product)) {
// Check if the current user is already one of the owners).
// If the current user is the owner then return to the product
// This line is not executed because in (products/show.blade.php) we have set a condition.
return redirect('products/' . $request->product);
}
$join = new Join;
$join->role = $request->join_role;
$join->product()->associate($request->product);
$join->user()->associate(Auth::user());
$join->message = $request->message;
$join->save();
// TODO: we have to make this with ajax instead of normal form
return redirect('products/'. $request->product);
}
You can send URL parameters to a POST request. Just make sure in your form you are sending the wildcard.
<form action="/products/{{ $productid }}/storeOwner" method="POST">
In your routes
Route::post('products/{productid}/storeOwner', 'ProductController#storeOwner');
In your controller, use it
public function storeOwner($productid)
{
dd($productid);
}
I'm trying to build a application in laravel 5.3 in which I get the variable from request method and then trying to pass that variable in a redirect to the routes. I want to use this variable in my view so that I can be able to display the value of variable. I'm currently doing this:
In my controller I'm getting the request like this:
public function register(Request $request)
{
$data = request->only('xyz','abc');
// Do some coding
.
.
$member['xyz'] = $data['xyz'];
$member['abc'] = $data['abc'];
return redirect('member/memberinfo')->with('member' => $member);
}
Now I've following in my routes:
Route::get('/member/memberinfo', 'MemberController#memberinfo')->with('member', $member);
Now in MemberController I want to use $member variable and display this into my view:
public function memberinfo()
{
return view('member.memberinfo', ['member' => $member]);
}
But I'm getting an error in the routes files
Call to undefined method Illuminate\Routing\Route::with()
Help me out, how can I achieve this.
When you're using redirect()->with(), you're saving data to the session. So to get data from the session in controller or even view you can use session() helper:
$member = session('member'); // In controller.
{{ session('member')['xyz'] }} // In view.
Alternatively, you could pass variables as string parameters.
Redirect:
return redirect('member/memberinfo/xyz/abc')
Route:
Route::get('/member/memberinfo/{xyz}/{abc}', 'MemberController#memberinfo');
Controller:
public function memberinfo($xyz, $abc)
{
return view('member.memberinfo', compact('xyz', 'abc'));
}
You can use like this:
route:
Route::get('/member/memberinfo', 'MemberController#memberinfo')
and the redirect:
return redirect('member/memberinfo')->with('member', $member);
You need to replace => with ,
public function register(Request $request)
{
$data = request->only('xyz','abc');
// Do some coding
.
.
$member['xyz'] = $data['xyz'];
$member['abc'] = $data['abc'];
return redirect('member/memberinfo')->with('member', $member); // => needs to be replaced with ,
}
Hope this works!
Replace line
return redirect('member/memberinfo')->with('member' => $member);
to
return redirect('member/memberinfo')->with('member', $member);
......
I want to pass multiple parameters from route to controller in laravel5.
ie,My route is ,
Route::get('quotations/pdf/{id}/{is_print}', 'QuotationController#generatePDF');
and My controller is,
public function generatePDF($id, $is_print = false) {
$data = array(
'invoice' => Invoice::findOrFail($id),
'company' => Company::firstOrFail()
);
$html = view('pdf_view.invoice', $data)->render();
if ($is_print) {
return $this->pdf->load($html)->show();
}
$this->pdf->filename($data['invoice']->invoice_number . ".pdf");
return $this->pdf->load($html)->download();
}
If user want to download PDF, the URL will be like this,
/invoices/pdf/26
If user want to print the PDF,the URL will be like this,
/invoices/pdf/26/print or /invoices/print/26
How it is possibly in laravel5?
First, the url in your route or in your example is invalid, in one place you use quotations and in the other invoices
Usually you don't want to duplicate urls to the same action but if you really need it, you need to create extra route:
Route::get('invoices/print/{id}', 'QuotationController#generatePDF2');
and add new method in your controller
public function generatePDF2($id) {
return $this->generatePDF($id, true);
}
I want to visit a page like...
http://mysitelocaltion/user_name/user_id
This is just a virtual link, I have used a .htaccess -rewrite rule to internally pass "user_name" and "use_id" as get parameters for my actual page.
How do I achieve the same in Laravel?
Update:
This shall help (documentation)
Route::get('user/(:any)/task/(:num)', function ($username, $task_number) {
// $username will be replaced by the value of (:any)
// $task_number will be replaced by the integer in place of (:num)
$data = array(
'username' => $username,
'task' => $task_number
);
return View::make('tasks.for_user', $data);
});
Route::get('(:any)/(:any)', function($user_name, $user_id) {
echo $user_name;
});
Great to see you using Laravel!
You can add the following in your route
Route::get('user_name/{user_id}', 'YourControllerName#method_name');
In your controller you can access the value as follows
public function method_name(Request $request, $user_id){
echo $user_id;
$user = User::find($user_id);
return view('view_name')->with('user', $user);
}