简体   繁体   中英

configuring routes.php in Codeigniter

I'm just starting a new project alone and I'm stuck in Codeigniter's URI configuration.

I'm using xampp, and so far I have a folder with my site in it with this path:

c:/xampp/htdocs/new_admin/site/application
c:/xampp/htdocs/new_admin/site/system

As it says in Codeigniter's manual, I added to the config.php the base URL which I consider is this:

$config['base_url'] = 'http://localhost/new_admin/';

I left index_page in blank:

$config['index_page'] = '';

I want my URI to look like:

http://localhost/new_admin/[controller]/[method] 

for example:

http://localhost/new_admin/admin/index

So in the file routes.php I did this:

$route['default_controller'] = 'admin';
$route['(:any)'] = 'admin/index';

But it's not even showing the result of the method index. The error is 404.

Since I can't find solution in documentation, I think I'm missing something.

How can I do to make this work as I want?

Update: I wrote this in .htaccess file

#Deny from all

RewriteEngine On   

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

ErrorDocument 404 /index.php

So after a day of trying I discovered that I was wrong when configuring my virtual host in

c:/xampp/apache/conf/extra/httpd-vhosts.conf

This is how it is now and working:

<VirtualHost *:80>
  ServerAdmin webmaster@localhost
  DocumentRoot "C:/xampp/htdocs/new_admin/site"
  ServerName local.new_admin
</VirtualHost>

<Directory "C:/xampp/htdocs/new_admin/site">
  Options Indexes FollowSymLinks
  AllowOverride all
  Order Deny,Allow
  Deny from all
  Allow from 127.0.0.1
  # to allow access from my local network add this line so other PC on my   network are allowed in but not anything from the internet
 # If you want the world to be allowed in uncomment this line
 allow from all
</Directory>

Then on

c:/windows/system32/drivers/etc/hosts

I added this lines:

127.0.0.1    localhost
127.0.0.1    local.new_admin  #this is my site

And after fixing that I added a new .htaccess file in this directory:

c:/xampp/htdocs/new_admin/site/

which contains:

php_value memory_limit 64M
php_flag magic_quotes_gpc 0 
php_flag magic_quotes_runtime 0

RewriteEngine On

RewriteBase /

#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]

# 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

Next to Codeigniter's application folder and system folder, in order to add the mod_rewrite permissions

Now to access localy to my project, I just type:

http://local.new_admin/admin/index

Hope this is usefull for newbies like me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM