Welcome Guest, Not a member yet? Register   Sign In
Weird URL structure, any ideas?
#1

[eluser]bapobap[/eluser]
Hello everyone,

Sorry my first post is a question, I'm hoping you can help!

I'm hopefully re-coding my app to use CI and have a question.

I can't change the URL structure so it has to match this, is this possible in a nice CI way, without using my existing somewhat sloppy code?

The URL's go like this: (everything gets sent to and is controlled by 1 "index" file

/category/sub_category/doctor_name/doctor_zip - this will serve a record for a doctor
/category/sub_category/doctor_name/ - this will serve all doctors of that name, anywhere
/category/sub_category/ - this will serve a list of sub categories
/category/ - a list of all categories

All parts of the URL are checked against a DB to check if they are valid, if not, the URL is considered a search. Also, if the first part of the URL exists as a php file in a certain directory, that will be served. The default is a search.

So if I have an about.inc.php page, this will be served via /about/, however if I were to type /muffins/ and that didn't exist as a file, or as a valid category in the DB, a search would be assumed. This means no 404 ever occurs as if none of these conditions are met, it is assumed they user wants to search for that term, much like PHP.net's search thing (but not coded the same, as no 404 header is ever sent).

Basically as the URL's are mostly dynamic and based on a database, I can't have controllers setup for every one. Is something like this possible and clean in CI?

Thanks for reading and any ideas you can provide!
#2

[eluser]JasonSTX[/eluser]
I think you can just use the URI class to pull each part out

http://www.yourdomain.com/index.php/cate...r/zip-code

$category=$this->uri->segment(1);
$sub-category=$this->uri->segment(2);
$doctor=$this->uri->segment(3);
$zip=$this->uri->segment(4);

Then just do a check to see what was given and compare it to your files or modules and if there is no match then run the correct query and show the results.

Uou can change .htaccess to redirect every request to index.php (via .*)

hope that helps.
#3

[eluser]bapobap[/eluser]
Thanks for that, where should I put all this logic, does this go in the "universal" controller?
#4

[eluser]JasonSTX[/eluser]
whatever your main controller is, inside the whatever class that extends Controller inside the index function.

so if $route['default_controller']='whatever' then:

controllers/whatever.php:

Code:
class Whatever extends Controllers {

function _construct {
parent::Controller();
}

function index {
//check the URI, compare to maybe an array of files or some other conditional
//then if it is an unknown and should be a query, send it to some model and then
//send the results to your view
}
}
#5

[eluser]Sarfaraz Momin[/eluser]
You can use CI routes to create these URLs. You can also use .htaccess just to remove the index.php from your URL. Let me know if you have any doubts and would try to post some code to help you.

Good Day !!!




Theme © iAndrew 2016 - Forum software by © MyBB