Welcome Guest, Not a member yet? Register   Sign In
URL question
#1

[eluser]Unknown[/eluser]
I'm trying to send a variable with the value of "the-project-name" to my project controller. It works right now when I send it to the index function:
http://mysite.com/project/index/the-project-name/

But how do I circumvent the index function and still send variables and launch the index function? So it looks like this:
http://mysite.com/project/the-project-name/

Doing this just gives an error: 404 Page Not Found The page you requested was not found.

Here's what my code looks like:
Code:
<?php
class Project extends Controller {
    function Project()
    {    
        parent::Controller();
        $this->load->model('Projects_model');
        $this->load->helper('typography');
        $this->load->helper('url');
        
    }
    function index()
    {
    
        
        $data = array(
              'project_title'   => 'Project',
              'project_heading' => 'All Projects',
              'query' => $this->Projects_model->get_project($this->uri->segment(3))->result()
              );
              
        $this->load->view('common/header');
        $this->load->view('common/nav');
        $this->load->view('project_view', $data);
        $this->load->view('common/footer');
        
        
    }
}
?>


How do you do this?
#2

[eluser]srobet[/eluser]
I'm using route to do that.
Code:
$route['project/(.*)']='project/detail/$1';

in my controller, I create the function detail instead the index.

Code:
function detail($project_name)
{
  ....
  your index functin code
  ....
}

and don't need to call the uri->segment in that function, just call the $project_name variable Smile

CMIIW
#3

[eluser]Sumon[/eluser]
The difference between
Code:
http://mysite.com/project/index/the-project-name/
and
http://mysite.com/project/the-project-name/
is index is the function name for first one and the-project-name for second one. So i think, only way to meet your expectation is by force add index/ in between. To do so i have tried by add in routes.php

[code]
$route['project/(.*)']='project/index/$1';
[code]

Not working. I feel, rewrite rule in .htaccess might be a solution. Sorry to say i don't know how to write .htaccess if (condition) execute instruction code.
#4

[eluser]srobet[/eluser]
I think rewrite rule just clean up the url.

You can try by type http://yoursite.com/index.php/project/the-project-name with no .htaccess.
#5

[eluser]Sumon[/eluser]
The question seems to me, 'the-project-name' treat as function name rather then parameter or segment while we remove index.php or index. So if we are able to place index in between http://mysite.com/project/ and the-project-name then the full uri path (apache treat) will become http://mysite.com/project/index/the-project-name.
Well at this point, lets consider the following .htaccess code
Code:
Options +FollowSymLinks
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^\.htaccess$ - [F]
RewriteRule ^favicon\.ico - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
The line
Code:
RewriteRule ^(.*)$ /index.php/$1 [L]
automatically add index.php just after yoursite.com. So if someone enter www.yoursite.com/prod_cat/prod_name apache get request as www.yoursite.com/index.php/prod_cat/prod_name.
Now to resolve our issue we need to write this sort of index placement in between but surely with a condition like: if the request url have project as first segment then we add index in the middle.
hope help you.
#6

[eluser]Michael Wales[/eluser]
Routes are what you want to use, something like this will work perfectly:

Code:
$route['project/([a-zA-Z0-9-]+)'] = 'project/index/$1';

Note: This will only match project/slug-here, not project/slug-here/ (note the trailing slash).
#7

[eluser]Sumon[/eluser]
I am using
Code:
$route['project/([a-zA-Z0-9-]+)'] = 'project/index/$1';

Here is my test controller.
Code:
<?php
    class Project extends Controller
    {
        function __construct()
        {
            parent::Controller();
        }
        
        function index($var)
        {
            echo $var;
        }
    }
?>
And i am getting the following error for http://localhost/shopno-dinga/project/test_value

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

But works fine for http://localhost/shopno-dinga/project/index/test_value
#8

[eluser]srobet[/eluser]
Hm.... I always use that method for uri manipulation and works.

Can you try new function instead index like :
Code:
function index()
{

}

function show_page($var)
{
    echo $var;
}

and reroute your routing to this function.

Tell me it works or not
#9

[eluser]Sumon[/eluser]
Thanks but the question is now how we can utilize CI route power by using
Code:
$route['project/([a-zA-Z0-9-]+)'] = 'project/index/$1';
#10

[eluser]Michael Wales[/eluser]
Sumon - you are saying routing does not work if you are routing to the index() method? For example, if you changed the route to show_page, like srobet suggested, it works?

Two thoughts here:
1) If my description above is correct, we should submit a bug report about it.
2) show_page is a much better name than index() because it actually tells you what the method does. I've always been a fan of routing and you will hear me say over and over - the class/methods have nothing to do with the URI. It's a nice side benefit but never rely on it, if you want a specific URI, define it. This is the perfect example where a method could be named much more descriptively and routing could do it's job.




Theme © iAndrew 2016 - Forum software by © MyBB