4

I now set uri routing in my codeigniter config like this

$route['user/:any'] = "users";

and everything works fine with request like this

http://localhost/user/1

but if i try request

http://localhost/user/

so uri segment is empty, i am getting error

404 Page Not Found
The page you requested was not found.

how to avoid this? I ve tried to use

$user = $this->uri->segment(2);
if (!isset($user)) redirect('/', 'refresh');

but this is not working.

4
  • 1
    Just out of curiosity, what is the '1' in the second segment for? Commented Mar 16, 2013 at 12:20
  • I think 1 can be only... administrator ;D Commented Mar 16, 2013 at 12:30
  • Just string now to test output, planning name/id Commented Mar 16, 2013 at 12:35
  • Still not working? Have you tried $route['user'] = 'users'; $route['user/(:any)'] = 'users/index/$1'; ? What is var_dump($slug); die(); in your index method in your controller users echoing when you access it like http://something.com/user/2? Commented Mar 16, 2013 at 12:38

4 Answers 4

5

You can add another route such as:

$route['user'] = "users"
Sign up to request clarification or add additional context in comments.

1 Comment

This is working, with if (empty($user)) redirect('/', 'refresh'); perfectly, only minus is addition route
4

build your functions in your controller instead of this:

public function index()
{
...

try something like this:

public function index($slug = '')
{

if ($slug = '') {
   // redirect code here
}
else {
   // if there is something in $slug then load this code
}

and maybe your routes should be something like:

$route['user'] = 'users';
$route['user/(:any)'] = 'users/index/$1';

7 Comments

Tried this index($slug = ''), isn't working because codeigniter uri routing only can get methord parameter as $this->uri->segment(2);
To get parameters for your method instead $route['user/:any'] = "users"; try this: $route['user/(:any)'] = 'user/index/$1'; as I suggested. WHat is the file name of your controller for users? user or users?
controller users :) tried $route['user/(:any)'] = 'users/index/$1'; uri routing works fine, but without parameter still 404
if you try: var_dump($slug); die(); as first thing after public function index($slug = '') { what happens? Are you shure that in browser you have links like: http://something.com/user/2 and not something like http://something.com/index.php/user/2 ?
After http://something.com/user/2 there should be then the value echoed. How do you access this method via browser? What is your url? You can change your domain name if you don't want to reveal it.
|
1

Try with

$route['user:any'] = "users";

Comments

1

I think you need to fix the syntax, please try:

$route['user/(:any)'] = "users";

You need parentheses around the wildcard (:any).

Reference: http://ellislab.com/codeigniter/user-guide/general/routing.html see Examples

4 Comments

I thought the same thing, but after some testing, :any also works. Although, it's definitively better to follow the documentation.
The CI documentation shows one example without parentheses. I think that the parenthesis are needed if you are trying to capture the segment value to reuse in the new route.
You are right, you can't use the back-reference if you don't put parenthesis. CodeIgniter supports :any, but doesn't automatically add parenthesis.
That makes sense, CI is probably relying on a regular expression parsing approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.