In my codignitor project I have the following view
public function index(){
$whatever = $this->request->getVar("value");
}
I have similar code all over my project, which was 100% working until today, when suddenly it stopped
now $whatever is NULL
However if I just change the code to use:
$whatever = $this->request->getGet();
$result = $whatever['value'];
result will be equal to the value....
Here value is a GET parameter in the url like: example.com?value=1
According to the docs:
"The getVar() method will pull from $_REQUEST, so will return any data
from $_GET, $POST, or $_COOKIE."
when I checked the value of $_GET I see my parameter as expected.
Is this a bug in codeignitor? (the strange thing is it only suddenly stopped working)
I ended up submitting an issue on github here: https://github.com/codeigniter4/CodeIgniter4/issues/4418
which was reportedly solved here:
https://github.com/codeigniter4/CodeIgniter4/pull/4493
Description In the current version, when the request is the get method
and the content-type is json, the $_REQUEST parameter cannot be
obtained
apparently it was an issue with that specific version (v4.0.4) that is now fixed.
In my case the $this->request->getVar() was not working inside models. As per the documentation of CodeIgniter 4:
"If you are not within a controller, but still need access to the application’s Request object, you can get a copy of it through the Services class"
So you would access REQUEST variables like:
$request = \Config\Services::request();
$some_field = $request->getVar('field_name'); // or getPost for $_POST array
Hope it would help others
Related
I need to call a method from CLI which uses the Request::input so I had to manually create the Request object and the below code is not working in my case. can you please advice.
$request = \Illuminate\Support\Facades\Request::create('script/run', 'POST');
\Illuminate\Support\Facades\Route::dispatch($request);
$request->request->add(['adjustment' => '10']);
I am trying to call the value from another class
dd(\Illuminate\Support\Facades\Request::input('adjustment'));
output shows as null but it supposed to show the value 10
I also tried the below option and it did not work as well
$request = app('request');
$request->request->add(['adjustment' => '10']);
dd($request->all());
returns empty array. If I am able to add input here, it will work for me.
You're almost there, but when you create the request you are assigning it to a variable, however, when you go to die and dump the value of 'adjustment' you're calling a new request which doesn't have the value of 'adjustment'
To obtain this simply call it from the request you've assigned to $request.
dd($request->input('adjustment'));
Found the answer finally, I had to use merge instead of add. below code worked for me.
$request = app('request');
$request->merge(['adjustment' => '10']);
dd(\Illuminate\Support\Facades\Request::all());
This is my first Laravel project, i'm making a routing mistake and have been trying to solve it for days now, solution must be very simple, so i'm missing something. I attempted some similar solutions I found in other posts here, but they contained much more complex code and only made things worse for me at this point.
I have a GET route like:
Route::get('peppers/{id}', function ($id) {
//return $id;
$pepper = DB::table('peppers')->get();
return view('peppers',['id' => $id,'pepper'=>$pepper]);
})->name('peppers');
And I also have a simple POST route. (for a form that I used on another page)
It inserts everything correctly into my database. The problems is in the redirect.
Route::post('pepperCreate', function (\Illuminate\Http\Request $request) {
$post = $_POST;
$pepperType = $_POST["pepperType"];
$pepperURL = $_POST["pepperURL"];
$pepperAuthor = $_POST["pepperAuthor"];
and so on... ending with:
return redirect()->route('peppers/{id}', [$id]);
})->name('pepperCreate');
But every time i try to redirect, it gives me an error saying:
Route [peppers/{id}] not defined.
(My question is: Did i not just define that GET route, because it works when I click into that URL) with:
{{$topic->topicTitle}}
After days of trying different variations of this, i'm trying to figure out what i'm missing here. Any input is appreciated.
Indeed, because you need to use your route's name, and use an associative array for the parameters too, as in:
return redirect()->route('peppers', ['id' => $id]);
Hope this helps!
I'm writing a PHP 5.6 application using apigility 1.0.4 and zend framework 2.3.3
with apigility I created a new reset service called drink
and created a filed called "drink_flavor".
I used the following filters:
Zend\Filter\StringToLower
Zend\Filter\StringTrim
now I use postman to test it.
so i configured the url to http://url/drink
I'm sending post data with raw json with the following text:
{"drink_flavor" : " AAA"}
as you can see i have a space at the beginning and the letters are capital.
now if my controller code i have the following:
public function create($data)
{
die(var_export($data,1));
}
so i'm just printing the data.
if I understood everything correctly instead of getting ' AAA' i should have gotten 'aaa'
because of my filters but I still got the unmodified data which is " AAA".
any ideas what's missing?
You have to pull the data from you InputFilter to get the filtered data.
So inside your listener:
// Get filtered data
$inputFilter = $this->getInputFilter();
$data = $inputFilter->getValues();
And continue to use that $data array instead.
The $data param in your create method is the raw/unfiltered POST data.
You should be careful using that $data as a source for your methods since anything sent by the client will be in there.
I think this is not so clearly explained in the Apigility documentation and I think that a lot of users make this mistake. I wrote about this in an issue on GitHub.
I'm creating an API using Cakephp 2.x that needs a POST request to post some data to the server however when I'm posting (using Postman) to 127.0.0.1/appname/api/confirm with code=123 in the post parameters my $_POST is an empty array.
My route works, I can see variables that I declare and output within the controller, and I've checked that the parameters are being passed in the request by using the chrome developer console checking the network data.
Router::connect('/api/confirm', array('controller' => 'awesomeController', 'action' => 'confirm'));
<?php
class AwesomeController extends AppController {
public function confirm() {
$this->autoRender = false;
$this->layout = 'ajax';
pr($_POST);
}
}
?>
I've got my endpoints for the get requests to work just fine, it only seems to be POST data.
Not quite sure why $_POST wouldn't even be available and I'm sure it's something ridiculously silly I've overlooked!
** Edit **
I've attempted the following without success:
$this->request->query
$this->request->data
$this->request->params
I have another method whereby I use GET along with ?parameter=value etc and I am able to use one of the above calls to retrieve the data.
In this case, the variables should be in
$this->request->query
Try using URLs like api/confirm?code=123, and they will be in request->query
I may be wrong since I am pretty new to cakePHP but since you set:
$this->autoRender = false;
so the view is not rendered automatically to set the view to ajax layout.
Isn't it necessary to call:
$this->render();
After setting the layout as said here?
Well, hope it helps.
If anybody come here one day by googling, just had the same problem.
Had a REST Controller, called with URL /rest/something/cool.json
Method called inside RestController.php, had output, but no POST, no REQUEST.
Tried with code=123, sending direct JSON, the only way to make it works was to set Content-Type to application/json and to send actual working JSON : Cake seems to validate prior to anything, sending raw data seems useless.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
CodeIgniter Routing
What should be happening: user navigates to URI, routes.php grabs the State and sends it to the controller, the controller returns some info from a database query. Pretty basic stuff.
The problem: the URI isn't passing the variable to the controller. I'm being told
Missing argument 1 for States::state_summary
I can set a default for the function argument, ie. ($st='Alabama') and everything works smoothly.
I don't even see how this is possible. Maybe at least tell me what I need to test to track down the bug.
URI:
http://example.com/index.php/states/Alabama
routes.php:
$route['states/(.*)'] = "states/state_summary/$1";
States controller:
...
function state_summary($st)
{
// DB query
// Return data
}
...
I believe your route should be adjusted to this:
$route['states/(:any)'] = "states/state_summary/$1";
That worked for me.
I'm not sure if (.*) is valid as I've never seen it used.
Well, I never write the controller to have parameter, instead I use rsegment method:
...
function state_summary()
{
$st = trim($this->uri->rsegment(3));
// DB query
// Return data
}
...
With this, I have more control with the passed parameter. I can sanitize it using trim or intval, before pass it to model or library.
Also, there are some tweak in codeigniter core library about routing the url. See it in the file system/libraries/Router.php, the code inside function _parse_routes() around lines 278. It is how URI routing work in CI.