Welcome Guest, Not a member yet? Register   Sign In
Routing doesn't work
#1

[eluser]Marty[/eluser]
Hi,
I have problem with URI routing in CI. I've read many topics about this, but still can't solve my problem. I have very simple app - one controller that shows some articles - its name is "homepage" and it is set as default controller. In this controller I have index function that show homepage site. Another function is called "show" and this function shows articels due to 3. uri segment, for example: www.example.com/homepage/show/my_article, where "my_article" is identificator for database. I have removed index.php from my URI shcema.

So, now I need to route URI, when I use www.example.com/my_article I want to call www.example.com/homepage/show/my_article

My code is:
Code:
$route['my_article'] = "homepage/show/my_article";

but it doesn't work (I allways get only index) and I really don't know where is the problem...
I've tried
Code:
$route[':any'] = "homepage/show/$1";
but that is the same...

I am using Code Igniter Version 2.0.2

Can someone, please, tell me, what I am doing wrong? Thx all.
#2

[eluser]Emkay[/eluser]
Try putting brackets around :any
#3

[eluser]pickupman[/eluser]
Routing runs until it finds a match. So you will need to define some other routes above the :any, otherwise only that will be run. Route override the default URI loading method.

Code:
$route['homepage'] = 'homepage/index';
$route['(:any)'] = 'homepage/show/$1';
#4

[eluser]Marty[/eluser]
Thx, I've tried both code, you posted, but it still doesn't work. Is it possible, that it could be server side problem? (htaccess or something else)
#5

[eluser]pickupman[/eluser]
Doesn't work doesn't help us figure put the problem. Are you getting an error or still just getting the same page served. You could post you. htaccess file to if there is something going wrong there.
#6

[eluser]Marty[/eluser]
Yeah, I'm sorry. I always get only index function of "homepage" controller, that is set as default. My htaccess file:

Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /novy/

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]
    
    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule>
#7

[eluser]pickupman[/eluser]
Just try something vanilla like this one.
Code:
RewriteEngine On
RewriteBase /novy/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Sometimes depending on the server environment, it's picky about having the question mark following index.php.
#8

[eluser]Marty[/eluser]
Thx, I changed htaccess to this, but still the same situation as before...
#9

[eluser]pickupman[/eluser]
Make sure you have both routes I posted before.
Code:
$route['homepage'] = 'homepage/index';
$route['(:any)'] = 'homepage/show/$1';

Also, could you post code from your controller, as maybe it something from there. Or try creating a test controller, and see what happens when you try and access it.

Maybe your /homepage/show method shows the hompage/index if the slug (article name) is not found?
#10

[eluser]John_Betong_002[/eluser]
>>> but it doesn’t work (I allways get only index)

Try inserting this script and post the output.

Code:
class homepage {

function index()
{
  echo __METHOD__;
  echo '<pre>';
    print_r( $this->uri->segments);
    print_r( $this->uri->rsegments);
  echo '</pre>';
  die;
  ...
  ...
  // old code
  ...
  ...
}//
function show()
{
  echo __METHOD__;
  echo '<pre>';
    print_r( $this->uri->segments);
    print_r( $this->uri->rsegments);
  echo '</pre>';
  die;
  ...
  ...
  // old code
  ...
  ...
}//

}/// endclass

&nbsp;
&nbsp;




Theme © iAndrew 2016 - Forum software by © MyBB