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!
Related
I am using CI3 HMVC in my project. Now, I am facing problem with routing. I want user to type www.demosite.com and it would automatically call my home module. I do not want to show like www.demosite.com/home. I want to show the url like www.demo.com. for this, I set default controller in application/config/routes, like this, as follows;
$route['default_controller'] = "home";
also in my content module, I added a route folder where I wrote
$route['home'] = 'home';
Here is my .htaccess
AddType text/x-component .htc
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !(index\.php|assets/)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
This is my home controller
class Home extends MX_Controller{
function __construct(){
parent::__construct();
}
function index($stub=""){
$baseUrl=base_url();
$this->load->helper("url");
echo $this->_showHomepage();
}
}
However, when I run this. I got 404 error. What can i do to solve this problem? Thanks in advance.
HMVC it should be like that
This only takes a method no directories are allowed
so the default controller is under controllers
$route['default_controller'] = 'pages/pages/view';
$route['default_controller'] = 'pages';
index is the default method that is called.
$route['home'] = 'authentication/home/index';
$route['home'] = 'authentication/home';
And than you need to change your .htaccess file to match that urls
URI Routing : https://codeigniter.com/user_guide/general/routing.html#examples
Update :
(defined('BASEPATH')) OR exit('No direct script access allowed');
class Site extends MY_Controller {
function __construct() {
parent::__construct();
}
function index() {
$this->load->view('url');
}
}
I solved it. I moved the Home folder from Modules folder to Controller folder and then, it just started to work
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
I've been working in a codeigniter application that was copied from windows to linux. It is 100% working in windows platform but when i tried to run it in my linux machine (Running on Elementary OS) it started to give me an error 404 for every controller aside from the default controller (Welcome) Controller. I edited my .htaccess to look like this one but it doesn't seem to work
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I already tried searching in the internet and try playing with my htaccess to work but same things happened.
This is my controller and routes
Home Controller.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class home extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('main_page');
}
}
as you see the controller only display a certain HTML file.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['default_controller'] = 'home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
Thanks, I've been stuck in this on this problem for almost 3 days. :( Hope that someone will help me out there. Thank you!
Changing the name of my controller to Uppercase the first letter works like a charm! I didn't notice that it was in the documentation.
I've a Subdomain under a maindomain. I want to install CodeIgniter under subdomain. But subdomain does not work for CodeIgniter default route.
Like my subdomain is demo.example.com not working. Normally core php is working for this subdomain but CodeIgniter not.
But when I call demo.example.com/welcome then working. Here welcome is a Controller name.
Can anyone help me what is my wrong ?
My Base URL is:
$config['base_url'] = 'http://demo.example.com';
My .htaccess is
RewriteEngine on
RewriteCond $1 !^(index\.php|public|\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
My default controller(welcome.php) is
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
echo 'Welcome Subdomain';
}
}
Your controller name is Welcome.php as a public_html/demo/application/controllers/Welcome.php.
Please change instead Welcome.php to welcome.php
Set instead $route['default_controller'] = "Welcome" to $route['default_controller'] = "welcome" in routes.php
If you have CodeIgniter install in your main domain add RewriteBase / in your .htaccess
class Users extends CI_Controller{
public function index(){
echo 'hello';
}
}
I kept the file name as Users.php, still its not working.
Please Consider about Codeigniter naming conversion.
and
in config.php
$config['base_url'] = '';
$config['index_page'] = '';
place .htaccess outside application folder
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
and
in routes.php
$route['default_controller'] = "users";//default controller name
and in URL
http://localhost/<path to file>/project_name/users
Note : You cant access URL with any file extensions like .php or .html.
start contract function in your class
class Users extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function index(){
echo 'hello';
}
}
And run the url as example.com/index.php/users/
This might help you out:
-> app/config/routes.php
$route['default_controller'] = 'pages/homePage';
-> app/controllers/Pages.php (Note the capital P on the file)
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Pages extends MY_Controller {//Note the capital Class name
public function homePage()
{
$this->load->view("pages/homepage.php");
}
}
Also to get rid of the index.php do the following :
-> app/config/config.php
$config['index_page'] = '';
Create a file called ".htaccess" outside the app folder and put the following code in it:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
This HTaccess is from another PHP framework called Laravel, it seems to be working better than the standard CI's HTaccess that they provide on their website.
I hope this helps you out.