I am finding for a good solution in codeigniter in where for every post request are made I would append something in it.
My temporary solution is to create a library and if post is found I would append something like this in my URI ?sTime=20140623101010 but this is dynamic which is basically bypassing the cache gateway that is setted in my server.
Any bright ideas that can lighten me up on this.
Thanks in advance
If your version of Codeigniter support it you can use CSRF protection to stop users reposting the same form more than once.. CI documentation Security ( if this is your problem.. )
If you want it for other reason there are different ways to create such "time" value.
Best way is to change the CI form Helper function form_open. Inside you can put what ever field you want and after that you have only to use this function when starting new form. CI documentation extending CI Helpers
Use before any function in your controller
if(!$this->input->get('time') && empty($this->input->post()))
{
redirect($url.'?time='.time());
}
Related
I have one trouble in routing in CakePHP. I have index action of all customers. And the question is, is there a way to make it this way, when I go to /..../.../customers CakePhp renders index(as per default), but when I am going to /..../.../customers.json(in .json format), CakePhp renders another action, where some array is serialized. I already enabled mapping resources, so it works just fine without overriding, but is there some way I can implement this ?
I've already read https://book.cakephp.org/2.0/en/development/rest.html.
Thank you, Gransfall. I just check if the request is json and then load the view in the need way.
if(isset($this->request->params['ext'])){
if($this->request->params['ext']=='json'){
//here setting serialized array
}
}
i want inside a controller and within the function init(){} to change the called action (requested action).
I mean if someone calls "www.mywebsite.com/myctrl/action1" i want inside the init function to call action2 instead, without redirecting the page and changing the url.
2- Is it possible to make a response inside init function, and stop calling the requested action ?
Best Regards
Wael
Why not just catch the URL in your config file and manually redirect it to the new action? It should work just fine and it would accomplish exactly what you want.
http://www.yiiframework.com/doc-2.0/guide-runtime-url-handling.html
I know this is not the way you wanted to do it but what is the advantage.
EDIT:
After you told me what you need to do then you really should not be doing this in this way. You should either be using AccessControl http://www.yiiframework.com/doc-2.0/yii-filters-accesscontrol.html or the beforeAction http://www.yiiframework.com/doc-2.0/yii-base-controller.html#beforeAction()-detail of the controller.
With the second option you can just throw an error and let Yii take care of the handling of it for you. The first one does kind of the same thing...
This problem is usually solved by using Apache's mod_rewrite or similar. There is no need to change your application at all. It's much more flexible than what yii2 has to offer and it'll be quicker because the web server is doing it natively.
The technique is generally used when migrating features, supporting different environments, sharing resources etc.
I have a Backend in Laravel, which is basically a REST API, because I'm using AngularJS in the FronEnd and making ajax requests.
Let's say I have to make a simply CRUD for Users
And I don't know if there is any difference between putting some of the parameters in the Route itself or all of them in the Form Input.
For Example:
Route::post('/Users/Update', 'UsersController#update);
And then call the 'id' parameter from:
Input::get('id')
or
Route::post('/Users/Update/:id', 'UsersController#update);
and include it as a parameter of the function update like:
public function update($id) { }
Is there any real difference between this two ways? maybe security issues? coding standards? or is it the same?
Should I just use Laravel's REST controllers?
If you are building a REST API you should have a URL like example.com/posts/42 and not example.com/posts?id=42 because it is cleaner and it is a coding standard.
I would also drop uppercase characters in your URLs and definitely go for your second choice of implementation. By the way, if you need to update a user you should use a PUT request like so: PUT users/:id.
The concise some-what opinionated answer:
You should define your route as:
Route::put('/users/:userId', 'UsersController#putUser');
Your public function putUser($userId) {} should return a 204 No Content on success.
The reasoning:
I've changed the route to be a PUT request to closer follow REST principles. Changing the controller method to putUser from update allows us to better define what the method is intending to do. While it seems trivial, it will help you distinguish between a PUT and PATCH update if you ever decide to implement one in the future. I used PUT as the method here but you can read about PATCH vs PUT and decide how far you want to go into following REST principles.
As for laravels restful controllers I feel they impose restrictions and add no real benefit so for REST api's I don't recommend using them.
Hey guys i have a little problem i need to solve and i cant seem to find a way to do so.
I have an app that need to use different databases dynamically according to which user uses it. I thought that i would give each user an URL that contains hes unique alphanumeric id. So the URL would be something like ww.mydomain.com/app/1kh1h3as/
So i have 2 problems:
where should i put the database switch code. Is it better of in config file or should i use it in model classes so i have use of URI class to parse out the id?
how can i make the router understand that it needs to offset all the calls by one segment so it wont go looking for 1kh1h3as controller and ww.mydomain.com/app/1kh1h3as/users/all will launch all() method within Users controller?
Use this
DB::connection('mysql')->config['database']='your_user_database_name';
Try
Route::any('app/1kh1h3as/(:bundle)', function() {
return 'Welcome to the Admin bundle!';
});
With simple queries I'm able to produce pages with nice pagination links. However, if the query is generated through some sort of search filter, I'm not sure how to pass the $_POST data from page to page.
Usually I can do this ... www.domain.com/search.php?id=200&type=host&rack=3&os=redhat%5
However, with CI's URI library, I'm using "pretty urls," hence my URL is more like www.domain.com/search/page/1 Appending the rest of the variables doesn't make sense nor will I think it will work. Any ideas on how to tackle this issue?
Why don't you just store the variables you need in a user session? Here is some information about how to use a session to store information about a user with codeigniter:
http://codeigniter.com/user_guide/libraries/sessions.html
Format your URI like so:
index.php/controller/method/id/200/type/host/rack/3/os=redhat%5/page/2
You can then use $this->uri->uri_to_assoc(n) to turn that URI into key-value pairs. See:
http://codeigniter.com/user_guide/libraries/uri.html
Please have a look the link below. This article show you have to pass parameters on pagination without turn on enable query string settings.
http://pujanpiya.com.np/?q=node/37
Hope this help!
If your using the built in pagination class, just make a separate search method on your controller and provide it with the pagination info...
FYI, there is a great jQuery Plugin that takes care of alot of table related work for you. Datatables.net
I just wrote a library that handles search, pagination, sorting and limiting.. Check it out here.
http://datatables.dyndns-web.com/