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
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'm starting to develop with CI, I'm trying to call another controller that I call LOGIN but it gets me the error "NOT FOUND", whereas if I put my controller in default_controller, it works, I already have read the other forum about this problem but it does not solve my case,
class Login extends CI_Controller {
public function index() {
redirect("/welcome/index");
$this->load->view("login");
}
}
Possible solutions
1. Please add .htaccess to the codigniter
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
2. Can create a routing for login controller then you can call directly
3. Add $config['index_page'] = 'index.php'; in config.php
Solution -1
class Login extends CI_Controller {
public function index() {
redirect("index.php/welcome/index");
$this->load->view("login");
}
}
Solution-2
application/config/config.php file you not remove index.php
$config['index_page'] = 'index.php';
change it
$config['index_page'] = '';
and using .htaccess index.php remove.
Reference link: index.php from URL
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
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.