I need to get the quantity of items from a form and pass that to CI's paypal_lib auto_form:
This is my controller:
function auto_form()
{
$this->paypal_lib->add_field('business', 'admin_1261513315_biz#pixelcraftwebdesign.com');
$this->paypal_lib->add_field('return', site_url('home/success'));
$this->paypal_lib->add_field('cancel_return', site_url('home/cancel'));
$this->paypal_lib->add_field('notify_url', site_url('home/ipn')); // <-- IPN url
$this->paypal_lib->add_field('custom', '1234567890'); // <-- Verify return
$this->paypal_lib->add_field('item_name', 'Paypal Test Transaction');
$this->paypal_lib->add_field('item_number', '001');
$this->paypal_lib->add_field('quantity', $quant);
$this->paypal_lib->add_field('amount', '1');
$this->paypal_lib->paypal_auto_form();
}
I have a library of my own that validates the input and redirects to auto_form on validation. I just need to pass the var $quant to the controller.
How can I achieve this?!
If you're redirecting directly to the auto_form controller method you can setup an argument there to pass your data in:
auto_form($quant)
Then, depending assuming you have no routes, rewriting, or querystrings 'on' (basically a stock CI setup) to interfere, and you are using the URL helper to redirect, you would do your redirect something like this:
redirect('/index.php/your_controller/auto_form/'. $quantity_from_form);
More on passing URI segments to your functions here.
Or if you're already using CI sessions in your application you can add the quantity value to a session variable for later retrieval inside of the auto_form controller method:
// Set After Your Form Passed Validation
$this->session->set_userdata('quant', $quantity_from_form);
// Retrieve Later in Controller Method After Redirect
$this->paypal_lib->add_field('quantity', $this->session->userdata('item'));
More on CI sessions here.
Related
I have a page where you can create your workout plan. Second page contains "pre-saved" workouts and I want them to load by passing parameters from second page to first. If you directly access first page, you create your workout plan from scratch.
// first page = https://prnt.sc/y4q77z
// second page = https://prnt.sc/y4qfem ; where you can check which one you want to pass to first page
// final step looks like this: https://prnt.sc/y4qh2q - but my URL looks like this:
www.example.com/training/plan?sabloni%5B%5D=84&sabloni%5B%5D=85&sabloni%5B%5D=86
this 84,85,86 are IDS
Can I pass params without changing URL ? Like having only /training/plan without anything after ?
public function plan(Request $request){
$workout = false;
if($request->workout){
$workout = $request->workout;
$workout = SablonTrening::find($sabloni); // $workout = array [1,3,4,5,6]
}
return view('trener.dodaj_trening', compact('workout'));
}
If you are getting to the /training/plan page with GET request, you could simply change it to POST. That way the parameters would be hidden in the URL but would be present in the request body. You would need a new post route:
Route::post('/training/plan', 'YourController#plan')->name('training.plan');
And then, in the form where you are selecting these plans, change the method on submit:
<form action="{{route('training.plan')}}">
//Your inputs
</form>
Your method should still work if your inputs stay the same.
Note: Not sure you would still keep the functionalities that you need, since I can't see all the logic you have.
If you have any questions, let me know.
To pass data from on blade to another blade.
At the end of first post before redirect()-route('myroute') add $request->session()->put('data', $mydata);
At the begining of the route 'myroute', just get back your data with $data = $request->old('data');
I have question about normal laravel multi form with post action as method based on laravel session. I added in this form component of vue, which is simple city autocomplete. I want load to inputs into fields of this component from session (session()), or old (old()) values, if session exist i want to load from session city and province, but if session doesnt exist, but old laravel values exist I want to load them in this component form, if they doesnt exist, leave fields empty. What's the easiest way to do that?
Get the session and pass a default which would be the old value.
$data = session('session_data_key', $old_value); // however you get the old value is up to you.
Pass it down to your view. If no session is set on the server, it will use the default (if default is null it will not set the auto-complete).
UPDATE
If you are using axios to make calls to a laravel app, before loading the form component (in mounted() {}) do a call to axios to get the value of (session or old value). This is what you'll load into the form.
axios.get('/api/load_dafault').then(resp => {
this.defaultValueToBePassedToField = resp.data.value;
});
The endpoint '/api/load_dafault' or whatever yours is named, will have it's controller like so:
public function loadDefaults()
{
$old_value = ... // however you get it is up to you.
$data = session('session_data_key', $old_value);
}
Do not forget to ensure you set the routes.
I've defined this resource in web.php of Laravel app,
Route::resource('Student', 'StudentCtrl');
and I have a variable which contains name of meta file,
$metaFile = 'student_meta.json';
I want to pass $metaFile to StudentCtrl, so I can catch the file name there.
I'm using Laravel 5.4.
Thanks.
Just pass the variable to the url, And thats it.
if its a get request will be routed to StudentCtrl#show
if its a put request will be routed to StudentCtrl#update
if its a delete request will be routed to StudentCtrl#destroy
And so on.
All you have to do is to define a method of the form. GET,POST,PUT/PATCH,DELETE.
Have a look here.
Passing a single variable from Router to Controller is possible only for Single HTTP request method.
Refer here : https://stackoverflow.com/a/50405137/9809983
For a complete resource route, its possible to send a fixed value to all views under that resource, if that's what you are trying to do
In Controller
public function __construct(Request $request)
{
$action_array = $request->route()->getAction();
$json_file['json_file'] = "student_meta.json";
$new_action_array = array_merge($action_array, $json_file);
$request->route()->setAction($new_action_array);
}
Now you can access the value in all the views under the resource controller like,
In View
{{ request()->route()->getAction('json_file') }}
Tested and perfectly works in Laravel 5.6
I want to use CController::forward() instead of redirect or instantiating the controller and directly calling the action, because this way Yii::app()->controller->action->id correctly shows the action that ultimately ran.
Although I don't see in the documentation how to specify parameters to pass to the forwarded action, because the $route parameter is a string, not an array.
public function actionIndex() {
$this->forward('/otherCtrl/view'); // how to pass a parameter here?
otherController.php:
public function actionView( $id ) {
//get the id here
Parameters injected into action method comes from $_GET. So if you need to pass $id into forwarded action, you need to set value in $_GET array:
$_GET['id'] = 'some id';
But using forward() is basically always a sign of bad design of application - I suggest to extract shared logic into separate method/component, and avoid using forward() or calling controller actions directly.
You can try:
$this->forward('/otherCtrl/view/id/'.$id);
Query string depends on your URL settings.
Hi I am trying to do the following in Laravel 5.2.
Summary: Pass some variables from middleware to view, via controller
Detailed: When a client is logged in, no matter what route they want, we need to check if they have completed all "setup steps" (each step refers to a different route, e.g. one could be company info, another could be product settings, etc). If they have completed all setup steps, then let them proceed to their chosen route, otherwise we redirect to the appropriate "setup steps" route, whichever one they haven't completed yet.
All client controllers run the same middleware, called NonSuperAdmin. I would like to put the checking of "setup steps" in this middleware, and redirect from there as appropriate. If client is redirected to a setup route by this middleware, we need the "incompleteSetupTasks" collection to be passed on to the relevant view, via the appropriate setup steps controller method.
Is this possible? Any help is much appreciated.
In the middleware use session handler
if($condition === true){
$data = [ //your content ];
Session::flash('your_key', $data);
}
next($request);
This data will also be available in your controller and in view
This is how you can access data in controller
public function yourControllerAction($request)
{
$somevariable = Session::get('your_key');
$viewdata = [
'content' => $somevariable
]
return view('yourview', $viewdata);
}
Or directly access the session data in view
//inblade
<p>
Your html content
#foreach(Session::get('your_key' as $data)
//your stuff
#endif
</p>
May be use Laravel Session to store and read values?
You can pass your setup steps to get or post parameters and check in routes with middleware if these parameters are empty:
Route::get('post/{setup1?}/{setup2?}', ['middleware' => 'role:admin', function ($setup1, $setup2) {
if(empty($setup1) and empty($setup2)){
// do smth
} else {
// redirect
}
}]);
Question marks mean, that they are optional parameters. Hope it was helpful.