Best way to do this?

#1
[eluser]Curtis.[/eluser]
Hi everybody,

I've just started using codeigniter, and everything has been good so far, but now I'm stuck on this.

I'm writing a simple search function, and I want the url's to look like this
http://localhost/index.php/controller/search/Search+Terms

So that they're easy to read and people can bookmark the search results.

Right now I have it partially working using 3 function
Code:
function index()
    {
        $this->load->view('form_view');
    }
    
    function search_redirect()
    {
        $search_string = $this->input->post('search');
        redirect('/reader/search/'.urlencode($search_string));
    }
    
    function search()
    {
        //search code
    }

And it works okay, the problem is that as soon as I add spaces to the search I get this:
Quote:An Error Was Encountered
The URI you submitted has disallowed characters.

I was wondering what the best solution is?

So far the only thing I have thought of would be to set $config['permitted_uri_chars'] to nothing, but the config file has a warning:
Quote:Leave blank to allow all characters -- but only if you are insane
I was wondering, what would be the problem with that?

#2
[eluser]Christophe28[/eluser]
Just below $config[‘permitted_uri_chars’] you can set $config['enable_query_strings'] to TRUE. Perhaps this is what you are searching for?

Christophe

#3
[eluser]Curtis.[/eluser]
Hi Cristophe,

I looked at that, but then I saw the warning in the user guide. The rest of my application uses the segment approach, and I don't want to mess up all my url's.

#4
[eluser]cahva[/eluser]
It wont mess up your urls and you can still use the segment approach. It just means if you really want to use the method index.php?c=controller&m=method, you cant use url helpers to create urls. But thats not a problem as you will still be using the normal segment method and you'll have GET variables at your disposal:
Quote:http://localhost/controller/search?q=Search+Terms

With that you can access that search query with $this->input->get('q').

#5
[eluser]pbreit[/eluser]
You might try adding + to permitted_uri_chars. Can you see which character might be the problem?

My understanding is that enable_query_strings does switch your URLs from segments to querystrings so that's not a great option (http://ellislab.com/codeigniter/user-gui.../urls.html). There are some threads on the forums about how to get around CodeIgniter's annoying lack of querystring support.

#6
[eluser]Agustín Villalba[/eluser]
I think your problem it's not related with enable_query_strings, I think your problem is this line
Quote:redirect('/reader/search/'.urlencode($search_string));
because the CI function redirect does the urlencode for you, so you are doing a double urlencode, and that raises the error. Try this:
Quote:redirect('/reader/search/'.$search_string);

#7
[eluser]cahva[/eluser]
[quote author="pbreit" date="1288775238"]
My understanding is that enable_query_strings does switch your URLs from segments to querystrings so that's not a great option (http://ellislab.com/codeigniter/user-gui.../urls.html). There are some threads on the forums about how to get around CodeIgniter's annoying lack of querystring support.[/quote]

Have you actually tried it? That sentence in there just means that the url helper will not work if you use the ?c=controller&m=method. But it does'nt mean you have to use ?c=controller&m=method as your urls and you can continue to use segments.

If you run Apache with mod_php this is the config that is needed:

Code:
$config['uri_protocol']    = "PATH_INFO";
$config['enable_query_strings'] = TRUE;

When run with CGI, theres couple of changes. In .htaccess and the uri_protocol config, but it will work nicely after those changes.

I have developed many sites with this that need GET(for example ecommerce sites that need get as most payment gateways return information only via get parameters) and have had zer0 problems. So in my book, codeigniter does have proper querystring support. Maybe the userguide needs some more detailed information as people are afraid to use it.

#8
[eluser]pbreit[/eluser]
If that works, then, yes, the user guide definitely needs more detailed information. Not only do you lose the HTML helpers, you also lose the form helpers. And who knows what else.

#9
[eluser]Curtis.[/eluser]
@pbreit

Adding + to the allowed characters would work for spaces, but I don't want to have to try and add every character possible.

@Agustín Villalba

I think your wrong about that, I've experimented with it, and Codeigniter doesn't urlencode it.

@cahva

Thank you, I made the changes you suggested and everything seems to be working now.

For anyone else who is wondering, the URI class is still working fine in my application. I haven't tried the html or form helpers yet.

#10
[eluser]cahva[/eluser]
[quote author="pbreit" date="1288834604"]If that works, then, yes, the user guide definitely needs more detailed information. Not only do you lose the HTML helpers, you also lose the form helpers. And who knows what else.[/quote]

I'll say this one more time. The helpers wont be any use ONLY if you use the ?c=controller&m=method way. I think you've misunderstood the userguide. The userguide means that you cant create querystring url's with them(meaning ?c=controller&m=method style urls) as they only work with segments. But that is alright, who cares if it doesnt create ?c=controller&m=method type urls as nobody in their right mind uses them.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.