Question

I'm VERY new to CodeIgniter and am having trouble calling a function from within my default class.

Controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Main extends CI_Controller 
{

public function index()
    {
    $this->welcome();
    }

public function welcome()
    {
    $this->load->view('view_welcome');      
    }
}

htaccess file

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /pickme/

RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

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

# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php

At the moment it is calling this Main controller by default as i would expect.

e.g

localhost/sitename
AND
localhost/sitename/index.php
AND
localhost/sitename/index.php/main/index
(All have the same result)

So i was under the impression that

localhost/sitename/class/function/id

would be how htaccess would be working but im getting a 404 error when i try and access

localhost/sitename/main/welcome
Was it helpful?

Solution

Try using the rewrite rule suggested in the user manual.

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Im not an expert with apace rewrite module, but yours definately looks wrong. You wouldn't want your application and system folders to be accesible from a browser. The best practice is actually to keep it outsite your web root folder.

Furthermore i see no reason to call the welcome method from the index method, instrad of just keeping the logic in the index controller.

But your idea of:

hostname/folder/class/function/parameters

Is very true :)

And last but not least, you have probably forgot to leave index file in your config.php blank, which you will need to do if you want to remove index.php.

Good luck (:

EDIT:

Try using this in your htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ ./index.php/$1 [L]
</IfModule>

And in your config.php remember to add a trailing slash.

With this it works here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top