Reputation: 303
I have some website http://www.example.com
, I have a controller abc
in which I have method index() which loads my website's view. I have made my controller abc
as default controller so that when user enters example.com
, he can directly see the view. I cannot change this default controller in any case. Now I want that if user enters example.com/1234
, where 1234 is profile number, so it should show that profile . if it is example.com/5678
, then it should show 5678's profile. The problem I am facing is , if user enters example.com/1234
then it will throw a 404 error because I don't have any controller 1234, even if I make a check in my default controller's index function if($this->uri->segment(3) == True)
it is throwing 404 error. Any help would be appreciated.
Upvotes: 0
Views: 1137
Reputation: 6852
Add in routes.php
$route["(.*)"] = 'abc/userid/$1';
Then in main controller abc add
public function userid()
{
$userid=$this->uri->segment(1);
echo ($userid);
}
Now you have userid in this function :)
Upvotes: 0
Reputation: 46
Create a pre_controller hook in config/hooks.php. Remember to enable hooks in config/config.php if you haven't already.
Example:
$hook['pre_controller'][] = array(
'class' => 'Thehook',
'function' => 'check_for_profile',
'filename' => 'thehook.php',
'filepath' => 'hooks'
);
Then create your hook method check_for_profile() in hooks/thehook.php. This method can check for a matching profile and display or redirect accordingly. If no profile match is found you can call the show_404() method.
This way, your existing controller/method paths are unaffected, and all of your routes remain intact.
If you're redirecting within the hook, use this format:
header("Location: controller/method");
..rather than
redirect('controller/method');
...as the latter will result in a continuous loop
Upvotes: 0
Reputation: 11338
In your routes file add this change:
$route['(:any)'] = 'abc/index/$1';
Then in abc controller:
public function index($profile=NULL)
{
$profile = $this->uri->segment(1, 0);
echo($profile);// just for checking, of course, you will remove this later, + the rest of your code, related to user id
Upvotes: 2
Reputation: 4079
Typically there is a one-to-one relationship between a URL string and its corresponding controller class/method. In the routes.php (config folder) you can change the defoult controller and routing also. Read the Wildcards (secion) in documentation below More informacion read the documentation
Upvotes: 0