When I am using url as
mysite.com/index.php/user/user the method is getting called.
But when I add below line in routes.php so that I can access the function using custom url.
it is throwing me error. 404 Not Found
I want to access the method using url as mysite.com/user
$route['user']['get'] = 'user/user';
user.php is present inside controller directory
<?php
use Restserver\Libraries\REST_Controller;
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . 'libraries/REST_Controller.php';
require APPPATH . 'libraries/Format.php';
class User extends REST_Controller {
function __construct()
{
// Construct the parent class
parent::__construct();
}
public function user_get(){
$this->set_response("request recieved", REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
}
}
[2nd question]
how to make a route to access the controller present inside certain folders in controller directory.
suppose a controller file is present in controllers/api/v1/ directory with file name user.php
Note:
I have tried all the solutions given by users on other posts but issue was not resolved.
EDIT: Issue Resloved
Routing is working just fine now. I think the problem was, I was calling mysite.com/user , instead I should have called mysite.com/index.php/user
Second issue of index.php being in the url is also resolved.
I was making the changes in .htaccess file which was present in Application folder instead.
I created a .htaccess file in root folder then added below mentioned line of code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
You have set your route as
$route['user']['get'] = 'user/user';
Which is directing to the user controller and the user method. But in your controller you have a method called user_get.
So the simplest fix here is to change your route to point to the correct method.
NOTE: You have no method called 'user' so why is that in the route?
So this...
$route['user']['get'] = 'user/user'; // the user method does not exist
Would become...
$route['user']['get'] = 'user/user_get'; // the user_get method does exist
Update: To remove index.php from your URL, your .htaccess might be
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
And make sure you add in your base_url in applications/config/config.php
$config['base_url'] = 'http://example.com'; // Change to your sites URL
$config['index_page'] = ''; // remove index.php
Related
I am assisting with a Codeigniter website and don't have a background with it. It has been noted that there is duplicate page content. When I dug in, they are not actual pages, but controllers pointing to the same views. I attempted to set up 301 redirects in an .htaccess file in the root of the website, but it is not taking. In the application/config/routes.php, the following is noted:
$route['default_controller'] = 'Home';
$route['404_override'] = 'Home';
For example, there is a page http://mywebsite.com/weddings, and then http://mywebsite.com/Home/weddings was another controller that pointed to the same view. I attempted to add a 301 redirect from Home/weddings after removing the additional controller pointing to it, but now Home/weddings just redirects to the home page, as it is set up with the 404 override. I hope I have provided enough information for some assistance.
.htaccess file:
RewriteEngine On
RewriteBase /
# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [R=301,L]
# allow access to certain directories in webroot
RewriteCond $1
!^(index\.php|robots\.txt|css/|lib/|js/|images/|^(.*)/images)
# gets rid of index.php
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# page redirects
RedirectMatch 301 ^Home/weddings/$ /weddings
Home.php is as follows:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Admin_model');
$this->load->model('Home_model');
$this->load->helper('captcha');
$this->load->library('recaptcha');
}
public function weddings()
{
$this->load->view('Template/Home/front_header');
$this->load->view('Home/weddings');
$this->load->view('Template/Home/front_footer');
}
Do not touch .htaccess. You have two options.
Create a controller Weddings.php and in your index.php return a
view for weddings page.
The other option is to use Home.php controller and then add weddings method, the method should return the weddings page view. Your route should look like below:
$route['default_controller'] = 'Home';
$route['404_override'] = 'Home';
$route['/weddings'] = 'home/weddings';
Hope this will help. Thank you
Edit
In your Weddings.php controller constructor check if there exist a "Home" and return 404 error. See below
function __construct()
{
parent::Controller();
if($this->uri->uri_segment(1) == 'Home') {
show_404();
}
}
I have a little problem with a tiny CodeIgniter app developed by another guy who had my job a while back. The app is already implemented, I just have to modify it a bit. The problem is that I can only access the two controllers that are already created. If I create another controller and I try to access it I get a 404 error.
Controller name: Test.php
Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller {
public function index()
{
die('test');
}
}?>
Result when trying to access it by myapp.com/test or myapp.com/index.php/test or myapp.com/index.php/test/index:
404 Page Not Found. The page you requested was not found.
Config/routes.php:
$route['default_controller'] = 'pages';
$route['brand'] = '/';
$route['brand/(:any)/(:any)'] = 'brands/brand/$1/$2';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
Pages.php and Brands.php are the two controllers already implemented.
.htaccess:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
If I try to access via url one of the other two controllers it works just fine. But I need to create new controllers for the changes I have to implement. What should I do? Where should I look for problems?
Thank you!
I have two codeigniter installations under my public_html folder on my server. One is directly under public_html and the other is under public_html/posts_receive.
The instance under public_html is working normal but when I try to access the codeigniter controller inside public_html/posts_receive it shows:
404 Page Not Found
tried url http://www.example.com/posts_receive/index.php/dashboard
To solve this problem I have added an .htaccess file under public_html/posts_receive which contain the following:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /posts_receive
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
</IfModule>
My controller is named dashboard.php and contains the following:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Dashboard extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library("pagination");
}
public function index() {
$config = array();
$data['link_sel']='dashboard';
$data['stats']=$this->fetch_stats();
$this->load->view("header_inc", $data);
$this->load->view("dashboard", $data);
$this->load->view("footer_inc", $data);
}
}
?>
config.php includes:
$config['base_url'] = 'http://www.example.com/posts_receive/';
$config['index_page'] = 'index.php';
You should run both applications from one CodeIgniter base. You will have 2 sub folders in your application folder. Take a look at the documentation: https://www.codeigniter.com/userguide3/general/managing_apps.html
From there on you can create the same index.php file as in your root in your posts_recieve folder and point it to the right application directory. That is how i managed to do it. Otherwise the CodeIgniter instance in your root will think you are navigating to a controller called posts_recieve which does not exists in that application.Hope this helps. Otherwise, just ask when things are unclear.
Inside .htaccess, RewriteBase /posts_receive part is not needed.
RewriteRule ^(.*)$ index.php?$1 [L]
should become
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
(<IfModule mod_rewrite.c> is a server configuration condition. It means you should have mod_rewrite module installed and active on the web server for these settings to work.)
You could leave empty $config['index_page'] inside config.php:
$config['index_page'] = '';
And you can call the controller without the index.php part:
http://www.example.com/posts_receive/dashboard
Have problem with CI 3.0
if i leave default controller in routes.php file "welcome" everything is working perfectly.
BUT if I change it i.e. "main" CI starts to throw 404 error
main controller for first steps is the same as welcome. I just copied files. renamed, changed class name (ofcourse), and in index() loading view.
any suggestions?
also I forgot to tell
on wamp localhost everything is working.. but in server NOT.. :/
And one more thing...
i.e. if I try go to mydomain.com/welcome - working,
if I try go to mydomain.com/main - NOT.
even if i change routes default controller back to welcome
My main.php file:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller {
function index(){
$this->load->view('welcome_message');
}
}
my routes.php file:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['default_controller'] = 'main';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
As said in the comments : Your controller's file name must start with an uppercase. In your case, Main.php.
See http://codeigniter.com/userguide3/changelog.html
«changed filenaming convention (class file names now must be Ucfirst and everything else in lowercase). »
You just need to set your default controller in application/config/routes.php like
$route['default_controller'] = $this->set_directory('front/home/').'home/index';
for Ci 3.x
i stuck with this problem.
i fixed it just replacing the path.
here "site" folder is the default if you want to use CI, you have to store your all code into "site" folder. if you have not used site folder for installation it ll not work with multiple controller setting.
hence you get default controller as a workable and rest of others appeared 404.
change .htacess file in your root directory.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule ^(.*)$ /site/index.php/$1 [L]
RewriteRule ^(.*)$ /codeig/index.php/$1 [L]
</IfModule>
above u can see there is
RewriteRule ^(.*)$ /codeig/index.php/$1 [L]
changed line for "codeig" root folder.
Try to change the function index() to Public.
If that doesnt work, then try to add in the URL: domain.com/index.php/main
and see what happens. Some times u need .htaccess in the other server to remove the index.php.
In Codeigniter 3 they are adding extra code to force the names of the files to start with an upper case letter. Here's a fix for this one.
In system/core/Router.php change the line (about 303)
if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php'))
to
if ( ! file_exists(APPPATH.'controllers/'.$class.'.php'))
I had to remove stuff in CodeIgniter.php and Loader.php as well.
I have the following url..
http://localhost/ci/site_controller/home
I want to remove site_controller controller from url resulting in..
http://localhost/ci/home
How can I do this in CodeIgniter ?
Note: If you'll ask what I've tried than I've just done searching over Google as I don't know how to use mod_rewrite.
EDIT
I have this in my routes.php
$route['default_controller'] = "site_controller/home";
$route['ci/home'] = 'ci/site_controller/home';
$route['404_override'] = '';
but still not working!
.htaccess
RewriteEngine On
RewriteBase /ci/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
ErrorDocument 404 /index.php
You can set your a route for each url:
In your config/routes.php file, just set each page like this:
$route['ci/home'] = "ci/site_controller/home";
This might help you to define a Default Controller for your CodeIgniter project.
https://codeigniter.com/user_guide/general/controllers.html#defining-a-default-controller
For instance, in your case, open your application/config/routes.php file and set this variable:
$route['default_controller'] = 'site_controller';
assuming you currently have your url http://localhost/ci/site_controller/home you can just write an explicit route like below to your /application/config/routes.php
$route['ci/home'] = 'site_controller/home';
or
$route['ci/home'] = 'ci/site_controller/home';
if your controller is namespaced to /application/controllers/ci/
This helped for me. In "routes.php" I added this:
$route['(:any)'] = "default_controller/$1";
try using the routes file to re-map url's
http://ellislab.com/codeigniter/user_guide/general/routing.html
I just found the solution in this link, it works for me: http://ellislab.com/forums/viewthread/148531/
$route['^(page1|page2|page3|page4)(/:any)?$'] = "YOURCONTROLLERNAME/$0";
Hope it helps you :)
In the case that your .htaccess file is set up like this (or similar), so index.php is already removed...
RewriteEngine On
RewriteCond $1 !^(index\.php|application|assets|images|js|css|uploads|favicon.png)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
And your codeigniter code lies directly in the html folder...
To remove the controller name from URL, edit application/config/routes.php
Add lines like these:
$route['signin'] = 'web/signin';
The line above calls function signin() of controller Web when user requests yoursebsite.com/signin
To pass variables into said function, do the following:
$route['p/(:any)'] = 'web/p/$1';
This line calls function p() in controller Web, which has a parameter. Function p() was defined as:
public function p($param = "none") {
echo $param;
}
I hope this helps :)
It's so simple if you want to remove the controller name from URL in the Codeigniter 3.x
in my case URL was: http://localhost:8080/index.php/page/sometext
Where "Page" is the controller name
open application/config/routs.php
$urlParam = $this->uri->segment_array()[1];
$rout[$urlParam] = "page/index";
Result: http://localhost:8080/index.php/sometext
I assure you, it will work. tested 100%.
Drop the 'ci' from your routes. That is your project name and is never required. Routes always start on the controller level:
$route['home'] = "site_controller/home";
Will work. However, more importantly.. are you sure your design is correct? Why not just let home be a controller? Create a /application/controllers/home.php and you'll be able to access it's index() function with http://localhost/ci/home.
You're complicating things. No need for routes at all.