Ajax pagination $_GET parameters in Controller - ajax

I am trying to understand how the Yii pagination for posts works in the Blog demo app and I see this request in the firebug console:
http://localhost/blog/index.php/post/index?ajax=yw0&Post_page=2
The function 'actionIndex' in the PostController does not seem to use the $_GET params. Where does the magic happen?

For such things you should check the source.
The index function would have a CActiveDataProvider whose fetchData function does this work.
Basically a CListView, or a CGridView calls the getData function of a data provider, which calls fetchData (say of CActiveDataProvider), which in turn calls CPagination's applyLimit, which calls getOffset, and this function calls getCurrentPage:
if(isset($_GET[$this->pageVar])) // this is where the $_GET is used

Related

Laravel resource collection paginate override

I'm working on a Laravel app and I use Resource collection with pagination to return responses for my apis calls. I want to be able to set the number of results returned per page from the URL. For example, if the url is like:
xyz.com/api/users?perpage=10
I know I can simply pass this number to ->paginate(10) but I want this to be dynamic for all my APIs. I think I have to override paginate() or maybe another method but I'm not sure. So instead of calling ->paginate(10) I would call ->paginate($request). My question is what methodI should override that can keep the original behavior of paginate and I would add this functionality to it?
You can pass perpage in the first argument of paginate function and have a default value if not provided.
User::paginate((int)$request->input('perpage', 15));

Whats the difference between redirect and this in Codeigniter?

I am new in Codeigniter and it's one of the good frameworks of php. But on some conditions I'm confused. Like this one. If any of you have any clarification about my dough, it's a great help for me.
Offcouse redirects refresh the page and $this not but apart from this I want to know - anyhow both of them used to go to somewhere else on view pages or like in other controller or in same controller to other methods.
But we don't use these side by side because when getting any of them it will go to that page or method without checking the next lines.
In case of a normal difference then have lot's of but I just want to know about the condition of going to next page or method when we use redirect or $this like this -
$this->Function($value); //It's method of same controller.
redirect('Controller/function'); //It's also doing same with page reload.
Thank for looking my problem.
Redirect()
When you will call any function of helper in codeigniter then you can call function directly without using any object. Helper in Codeigniter is collection of functions.
Redirect() method is a part of URL helper in Codeigniter.
For your ref. https://www.codeigniter.com/user_guide/helpers/url_helper.html
So, just load helper using $this->load->helper('url'); or you can also mention in autoload.php file.
$this->Function(); used to call a function from same controller
$this->Function(); used to call a function from same controller
redirect()
While building a web application, we often need to redirect the user from one page to another page. CodeIgniter makes this job easy for us. The redirect() function is used for this purpose.
redirect($uri = '', $method = 'auto', $code = NULL)
The first argument can have two types of URI. We can pass full site URL or URI segments to the controller you want to direct.
The second optional parameter can have any of the three values from auto, location or refresh. The default is auto.
The third optional parameter is only available with location redirects and it allows you to send specific HTTP response code.
Redirect means jumping to another function mentioned in the redirect method.
$this->Function($value); => jumping to another function and you can execute the code of the same function as well as pass the value back by returning value.
When you send request to codeigniter generally CI controller gets called and then function which is mentioned in uri segment. like below... So this will be another request.
redirect('Controller/function'); //It's also doing same with page reload.
But when you have to call another function within the same request then you can use below approach
$this->Function($value); //It's method of same controller.
This will execute the given function and return the value within same request.

Laravel $request->all() correctly returns data, but $_POST completely empty

I am making an ajax post request to the server, posting json data. In firebug I can see the network post call going through along with the json data.
In Laravel I was trying to do a simple var dump of the $_POST data and have just wasted a fair bit of time being confused as to why this should be completely empty. However, when I use the Request facade, my data is there.
ie. this just gives me an empty array:
public function test(){
Log::info($_POST);
}
...yet this prints my data, as I expect:
public function test(Request $request){
Log::info($request->all());
}
Why?
Edit
Thanks, #Webdesigner. The http verb is definitely post, as my method is called in my routes file via
Route::post('/image-upload', 'EntryController#test'); // Note "post" verb
I don't think $request->post() is valid in Laravel 5.4 as this throws an BadMethodCallException: Method post does not exist. error. However, I can confirm that
Log::info($request->method()); // POST
also tells me the method is post.
Very strange. I guess you're right that some part of the app is overwriting the $_POST global, though I have no idea why/where/how. Probably not relevant, but this call is being made from Angular 4.
Thanks for your help anyway!
This is not the normal behavior of Laravel. I tested this on a fresh Laravel 5.5 site and just did a Form submit and an Ajax POST request to the same Route.
Both give me the same result. A POST Request should have at least the CSRF Token as _token with a value.
One other point is $request->all() is not only the the content of $_POST so to have a fair compression you should try $request->post().
BTW only because you did a POST request do not mean that the data is send by the POST Method, it could be that the data you see in $request->all() is from $_GET and $_COOKIE, etc and only the Method was a POST.
Last but not least there it the option that some part of your APP is deleting the content of the Superglobal Variables. $_POST and the others are not like constants, so they can be changed during runtime e.g. $_POST = [];
I don't thing that there is a difference in Laravel 5.4.27.

Codeigniter method ajax issue related to url

I have a controller where i have a method called index.
In this method i am retrieveing data from database and using a paging library setting variables for view for paging. When i add a new record i am hitting another function using ajax. There after insertion i call
$this->index();
Now in index i am checking a condition
if($this->input->is_ajax_request()){
//dont load header and footer
}
but the problem is that when i come to index from my save function it looses the ajax request and my condition in index function in not checked and header and footer is always loaded. I want the ajax request still be available even if i jump from one method of codeigniter to another? Any suggestion? Or alter native.
Because i dont want to create another function where create the paging again with header and footer ommited.
Something that might be useful is CodeIgniter's session class which has a flashdata method. Flashdata is a bit of session data that is only stored for the next server request, then it is deleted.
In your save function, you could have this at the end:
$this->session->set_flashdata('ajax', true);
and as part of the condition in your index function, you could have:
if($this->input->is_ajax_request() || $this->session->flashdata('item')){
//dont load header and footer
}
This would then check that the request was actually an ajax request OR that a session variable has been temporarily set to tell CodeIgniter that it should be treated like an ajax request.

Run function on event

im trying to run javascript function when other funcion will stop.
For example: User change something in form and ajax has been sent in background. When ajax request is done i want to run function.
How can i do that ?
Or maybe there is a global trigger for ajax requests ?
You could achive that thanks to ajax asynchronous request and handler. If you, for example, call via ajax a page that do a particular function and then return, you can run your js.
Take a look at this: http://api.jquery.com/ajaxComplete/
You can configure a handler for the ajax request which gets called when you get response for the request. You can check the examples at http://api.jquery.com/jQuery.ajax/
If you want to call a specific function for a specific AJAX call, simply call it as part of the success or complete callbacks on that AJAX call. If, however, you want to call the same function whenever any AJAX request is finished, take a look at the .ajaxSuccess() or .ajaxComplete() methods in the jQuery API.
It looks that you are using jQuery, so you can use the "complete" or "success" settings to call code/function. Check the docs.
$.ajax({
url: "test.html",
success: function(){
// call another function
}
});

Resources