How can I pass a parameter via Route::controller to a controller?
I want to pass a parameter user_id to getCreate method in my controller
Route::controller('account', 'ReportController', array(
'getCreate' => 'client.account.create',
'postStore' => 'client.account.store',
));
Controller
public function getCreate($user_id = null)
{
// need to do something here with $user_id
}
You just call your url with the user id
www.mydomain.com/account/create/1
Related
I'm doing this query in the payment controller and i need to get a post request from the route.
Controller:
class PaymentController extends Controller
{
public function apiPaymentByUserId($date_from, $date_to) {
$payments = DB::table("casefiles, payments")
->select("payments.*")
->where("casefiles.id", "=", 'payments.casefile_id')
->where("casefiles.user_id", "=", Auth::id())
->where("payments.created_at", ">=", $data_from)
->where("payments.updated_at", "<=", $data_to)
->get();
return response()->json([
'success' => true,
'response' => $payments
]);
}
}
Route:
Route::post('/payments/{date_from}/{date_to}', 'Api\PaymentController#apiPaymentByUserId');
How to pass multiple parameters in this post route? Thank you
For post request no need to pass param in url .You will get in request
So route will be
Route::post('/payments', 'Api\PaymentController#apiPaymentByUserId');
and controller method
public function apiPaymentByUserId(Request $request)
{
$date_from = $request->date_from;
$date_to = $request->date_to;
}
If you do not want to change your url, try this in your controller apiPaymentByUserId() method, inject the Request object along with the other path variables like like:
public function apiPaymentByUserId(Illuminate\Http\Request $request, $date_from, $date_to) {
// ... you can access the request body with the methods available in $request object depending on your needs.
}
For POST request no need to pass param in url . Send the Dates as FORM values sent via POST method along with the rest of the FORM values (if any, you're already POSTing in the FORM). You will get all FORM values sent via POST method in Request $request object instance, passed in Controller/Method.
So route will be:
Route::post('/payments', 'Api\PaymentController#apiPaymentByUserId');
and controller method:
public function apiPaymentByUserId(Request $request)
{
$date_from = $request->date_from;
$date_to = $request->date_to;
}
I need to pass an additional parameter($uid) from my index.blade.php to my edit.blade.php by clicking on a button.
My index.blade.php:
Edit
My FlyersController:
public function edit($id, $uid)
{
return view('backend.flyers.edit')->withUid($uid);
}
With the code above I get an error: "Missing argument 2 for App\Http\Controllers\FlyersController::edit()"
What am I doing wrong here?
The error is not throwing from the action method. It is coming from route for that URL.
Check the URL for passing argument to the the controller.
If this is the your desire URL localhost:8000/backend/flyers/10/edit?%24uid=1 then the second argument is in $request variable not in controller function argument.
You should pass an array into action() helper:
action('FlyersController#edit', ['id' => Auth::user()->id, 'uid' => 1])
Ok,
the only way I can solve this is by using the following in My FlyersController:
public function edit(Request $request, $id)
{
return view('backend.flyers.edit')->withRequest($request);
}
and access then the uid with {{request->uid}} in my view.
If anybody has a better solution for this, let me know.
Use this code
return view('backend.flyers.edit', ['var1' => $var1, 'var2' => $var2]);
That will pass two or more variables to your view
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 have this:
Route::get('/product/{id}', 'PagesController#display');
And in my PagesController I have this:
public function display($id) {
return View::make("details", ["id" => $id, "premium" => $premium]);
}
How can I pass the variable $premium to the controller method without inserting it in the url? In simple words I don't want this: www.mysite.com/product/false/125 (false is the value of $premium) but this: www.mysite.com/product/125. That's why I have just $id as parameter in the controller method and no also $premium. I want to pass that variable $premium in other way.
I've try some approaches like this one:
Route::get('/product/{id}', 'PagesController#display')->where("premium" => "false");
which didn't work.
You can use a closure route and call the action "manually":
Route::get('/product/{id}', function($id){
return app('PagesController')->callAction('display', [$id, false]);
});
And
public function display($id, $premium) {
return View::make("details", ["id" => $id, "premium" => $premium]);
}
Is it possible to access route parameters within a filter?
e.g. I want to access the $agencyId parameter:
Route::group(array('prefix' => 'agency'), function()
{
# Agency Dashboard
Route::get('{agencyId}', array('as' => 'agency', 'uses' => 'Controllers\Agency\DashboardController#getIndex'));
});
I want to access this $agencyId parameter within my filter:
Route::filter('agency-auth', function()
{
// Check if the user is logged in
if ( ! Sentry::check())
{
// Store the current uri in the session
Session::put('loginRedirect', Request::url());
// Redirect to the login page
return Redirect::route('signin');
}
// this clearly does not work..? how do i do this?
$agencyId = Input::get('agencyId');
$agency = Sentry::getGroupProvider()->findById($agencyId);
// Check if the user has access to the admin page
if ( ! Sentry::getUser()->inGroup($agency))
{
// Show the insufficient permissions page
return App::abort(403);
}
});
Just for reference i call this filter in my controller as such:
class AgencyController extends AuthorizedController {
/**
* Initializer.
*
* #return void
*/
public function __construct()
{
// Apply the admin auth filter
$this->beforeFilter('agency-auth');
}
...
Input::get can only retrieve GET or POST (and so on) arguments.
To get route parameters, you have to grab Route object in your filter, like this :
Route::filter('agency-auth', function($route) { ... });
And get parameters (in your filter) :
$route->getParameter('agencyId');
(just for fun)
In your route
Route::get('{agencyId}', array('as' => 'agency', 'uses' => 'Controllers\Agency\DashboardController#getIndex'));
you can use in the parameters array 'before' => 'YOUR_FILTER' instead of detailing it in your constructor.
The method name has changed in Laravel 4.1 to parameter. For example, in a RESTful controller:
$this->beforeFilter(function($route, $request) {
$userId = $route->parameter('users');
});
Another option is to retrieve the parameter through the Route facade, which is handy when you are outside of a route:
$id = Route::input('id');