Passing Params to a page in key-value pair in CodeIgniter - codeigniter

I am using CodeIgniter, and I am new to it.
I came to know that I can pass parameters to another page like http://example.com/users/list/param1/param2/param3/.... and so on.
Now on the destination php script, I can get it as $this->uri->segment(2) , but I must know that which location it is in the uri. Say I am getting param2 , I must know that it is uri segment 4. This is a great pain for me. I previously worked with ZEND , where we send parameters like
http://examples.com/controller/action/key/value/key/value , Now in this case I can always dynamic number of parameters and I don't need to know their location in the uri, but I just need to know the key.
Qestion : Is there a way where I can pass parameters using GET in key-value pairs in codeigniter, like the one ZEND do?

Take a look at the function $this->uri->uri_to_assoc(n) in the URI class. It will take in your url segments as an associative array. You could then check whether the keys are present in it or not.
Example and full explanation in the user guide.

Related

How to remove parameters from url in laravel?

I am trying to remove the url parameters based on some conditions ,i can able to remove some of the parameters , i want to replace the some new value for the url, can you please give me some idea how to do this one
$request->query->remove('key');
$request->query->add(['admin'=>"true"]);
$request->request->remove('key');
$request->request->add(['admin'=>"true"]);
You almost done in a correct way, simply change the order of the queries then it will be working fine.
$request->query->remove('key');
$request->request->remove('key');

How to run single controller method from any route in Laravel 4

I really hope my question has been well thought out but here goes.
How do you implement something like
Route::get("/url1", "controller#method");
Route::get("hello/url1", "controller#method");
Route::get("hello/hi/url1", "controller#method");
in Laravel but using something like
Route::get("*/url1", "controller#method");
instead of declaring every route path?
I will explain why this problem has come up. You see the primary url is always changing because its being called from a js file via a location.href call. I could decide to use a primary url variable but its to be deployed via intranet to different servers in organizations and the primary url could change at any time meaning that localhost/project on one system might become localhost:7987/project on another thus breaking the url variable, now thats on one part. On the other hand there are js functions running continuously and when someone navigates to a deeper url, say from localhost/home to localhost/home/event a route call that should be independent of folder breaks
So yeah, I am wondering if theres a way to declare a global route that points to a controller and/or if this is possible in Laravel.
Thanks
Try this:
Route::get('{something}/url1', 'controller#method')->where('something', '*');
Not sure if that will work, but the idea is that you can use where to pass some Regexp to match selected value from route.

How to pass route values to controllers in Laravel 4?

I am struggling to understand something that I am sure one of you will be able to easily explain. I am somewhat new to MVC so please bear with me.
I have created a controller that handles all of the work involved with connecting to the Twitter API and processing the returned JSON into HTML.
Route::get('/about', 'TwitterController#getTweets');
I then use:
return View::make('templates.about', array('twitter_html' => $twitter_html ))
Within my controller to pass the generated HTML to my view and everything works well.
My issue is that I have multiple pages that I use to display a different Twitter user's tweets on each page. What I would like to do is pass my controller an array of values (twitter handles) which it would then use in the API call. What I do not want to have to do is have a different Controller for each user group. If I set $twitter_user_ids within my Controller I can use that array to pull the tweets, but I want to set the array and pass it into the Controller somehow. I would think there would be something like
Route::get('/about', 'TwitterController#getTweets('twitter_id')');
But that last doesn't work.
I believe that my issue is related to variable scope somehow, but I could be way off.
Am I going down the wrong track here? How do I pass my Controllers different sets of data to produce different results?
EDIT - More Info
Markus suggested using Route Parameters, but I'm not sure that will work with what I am going for. Here is my specific use case.
I have an about page that will pull my tweets from Twitters API and display them on the page.
I also have a "Tweets" page that will pull the most recent tweets from several developers accounts and display them.
In both cases I have $twitter_user_ids = array() with different values in the array.
The controller that I have built takes that array of usernames and accesses the API and generates HTML which is passed to my view.
Because I am working with an array (the second of which is a large array), I don't think that Route Parameters will work.
Thanks again for the help. I couldn't do it without you all!
First of all, here's a quick tip:
Instead of
return View::make('templates.about', array('twitter_html' => $twitter_html ))
...use
return View::make('templates.about', compact('twitter_html'))
This creates the $twitter_html automatically for you. Check it out in the PHP Manual.
 
Now to your problem:
You did the route part wrong. Try:
Route::get('/about/{twitter_id}', 'TwitterController#getTweets');
This passes the twitter_id param to your getTweets function.
Check out the Laravel Docs: http://laravel.com/docs/routing#route-parameters

Passing multiple values as query string is good or bad practice in MVC

In my page I ve to pass multiple values to controller.
My URL looks something like :
http://blah/Search/Page/?type=new&keywords=blahblah&sortType=Date
Is Passing multiple values as query string good practice in MVC ? or We can have slash separated URL, by using introducing custom routing?
NB: Lets consider all the values in my query string, are not secure / sensitive data.
I wouldn't consider it a bad practice, as long as it's not senstive data. It just depends if you want to write a custom route in your global.asax to handle it or not. The custom routes provide a cleaner url forsure. Also, for more savy users if they understand the concept on your site, it's more intuitive.
So consider this:
http://baseballcards/topps/1980 // search for baseball cards made by topps in the 1980s
http://recipes/deserts/pies // search for a desert recipe that are pies
http://code/csharpe/linq // search for csharp code that has linq examples
In these examples we can almost read the url like a sentence, making it more intuitive, giving a user the ability to plug and play. It clearly denotes the query almost like a breadcrumb, indicating exactly what the context will be. I personally like this. But either way is a good approach.
To extend with more parameters:
routes.MapRoute(
"SearchRecipes",
"Search/Recipes/{category}/{type}",
new { controller = "Search", action = "Recipes", category = "all" , type = ""}
);
Some examples:
Search/Recipes/Deserts/Pie
Search/Recipes/Dinner/Beef
Search/Recipes/Lunch/Salads
Select later (query string in route values) in case,
If you are concerned about header length.( By default get parameters are part of headers, and web server accept 1024 byte header length by default in IIS7).
Hide logical implementation of your code.
Url looks good and easier to remember.
Otherwise Both the approaches work equally.
I think passing search parameters in the query string is the way you should go, especially if all your parameters are optional. It also enables you to use normal method="get" forms without hassle.
I don't think "security/personal data" has anything to do with this since the query string is a part of the URL just like the path is.
IMO I think this is absolutely fine. I think it is actually preferable to use querystrings in MVC when the path represents the function and the querystring parameters represent the filters, e.g. as in a search.
However, I wouldn't use querystring parameters for data that represent information about the content retrieved. So I would include the year and month of an article in the path but not the page number if returning more than one page of articles for the year and month.

Can I have multiple inputs in a form in CodeIgniter URL?

I know that CodeIgniter already elegantly handles URL's. What I have is a form with multiple elements (date, keyword, location = optional). Is it possible to set up CI to create a URL that looks like:
mysite.com/class/function/date/keyword?
If you are using CodeIgniter's form helper, it sends your form data via POST, so you can't easily have your field's value displayed in the url.
What you could have, though, is a controller method that collects your form data and redirects to the url you want.
If you need further clarification, please let me know.
Yes, you can use the anchor function by passing the relevant info to it.

Resources