Anraiki
Anraiki

Reputation: 796

Url Segment but for Searching

The Url Segment in Codeigniter seems simple and easy to follow... however, I am creating a search feature which requires the Url Segment to be dynamic.

If I add a Search function within my Main Class, I would have to include the "Welcome" class to the Url Address: http://example.com/index.php/welcome/search/stuff-i-want-to-search/

However, I want the Url to be: http://example.com/index.php/search/stuff-i-want-to-search/

That is without the "Welcome" in the Url, and most likely also without the index.php

I thought I could achieve this by creating a new class file called Search. I am able to load the file but when I attempt to search, I end up in a 404.

Please advise.

Upvotes: 1

Views: 180

Answers (3)

tpae
tpae

Reputation: 6346

Using $route['search/:any'] = "welcome/search/$1"; is a risky process, because :any can take in anything..

I suggest using Regular Expressions, which is safer to use.

$route['search/([a-z_A-Z]+)'] = 'welcome/search/$1';

Upvotes: 1

Paulraj
Paulraj

Reputation: 3397

In CI, URI Routing will do this for you.

$route['search/:any'] = "welcome/search/$1";

Upvotes: 1

Ross
Ross

Reputation: 17987

you can do this with routing

http://codeigniter.com/user_guide/general/routing.html

i imagine:

$route['search/:any'] = "search/do_search/$1";

where by (i'm guessing) your do_search() function would take the term as a parameter, explode it at hyphens, and do what you need to do.

remember CI URLs are:

site.com./index.php/controller/function/id

Upvotes: 0

Related Questions