Codeigniter: 404 on existing controller - php

When I set $route['default_controller'] to 'blog', CI opens my controller without problems. When I want to access it via URL, CI returns a 404.
My controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Blog extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
}
Saved at
application/controllers/Blog.php
Some other config things:
$route['default_controller'] = 'blog';
$route['404_override'] = '';
$config['base_url'] = '';
URL I try to use:
http://localhost:63342/project/public_html/blog/
.htaccess to hide index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
How can I fix this issue? I tried a lot of solutions from similar questions without success

If your project is in project folder in main directory of the local server, change your address to:
http://localhost:63342/project/blog
You configured that page as your default controller, so it should work even by:
http://localhost:63342/project

Related

unable to load view from controller in phph - codeigniter

I am new to code igniter framework. I am having issue to call view from controller.
I have 3 three controllers in my application/controllers/ folder
employee.php, home.php and dashboard.php
and 5 views in my views/template/ folder
header.php, footer.php, sidebar.php, template.php and topmenu.php
and 3 views in my main views folder
addEmployee.php, home.php and dashboard.php
I am able to hit the home and dashboard controller, but unable to hit the employee controller to load addEmployee view.
this is my addEmployee.php view
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<title>Add Employee</title>
<div class="main-content">
<?php include 'template/topmenu.php' ?>
<!-- PAGE CODE STARTS BELOW FROM HERE -->
</div>
employee.php controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Employee extends CI_Controller {
public function __construct() {
parent::__construct();
$this -> load -> model('employee_model');
}
public function index() {
$data['header'] = 'template/header';
$data['sidebar'] = 'template/sidebar';
$data['main_content'] = 'addEmployee';
$data['footer'] = 'template/footer';
$this->load->view('template/template',$data);
}
function functionToTestgetAndSaveEmployeeDetailsResult() {
$result = $this -> getAndSaveEmployeeDetails();
print_r($result);
}
}
?>
template/template.php view
<?php defined( 'BASEPATH') OR exit( 'No direct script access allowed'); ?>
<?php
$this->load->view($header);
$this->load->view($sidebar);
$this->load->view($main_content);
$this->load->view($footer);
?>
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
config/config.php
$config['base_url'] = 'http://localhost:8050/test/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
I am using this url to access the views
http://localhost:8050/test/ for home
http://localhost:8050/test/dashbaord for dashbaord
http://localhost:8050/test/addEmployee for addEmployee
home and dashboard views work by this but addEmployee doesn't.
I have also tried these url but no luck
http://localhost:8050/test/employee/addEmployee
http://localhost:8050/test/index.php/addEmployee
http://localhost:8050/test/index.php/employee/addEmployee
Any idea, what is wrong with this addEmployee view? or any link ?
Seems like you might be using the wrong URL. To access the page that makes "addEmployee" the "main_content" try
http://localhost:8050/test/employee
That URL will run Employee::index()

Codeigniter subdomain 404 Not Found

I'm new to codeigniter and I'm working on a project that is done on this framework. When I try to access a url such as "mysite.com/about", it gives me and an error
404 Not found,
I tried many .htaccess file on wamp and a live host but the result is the same.
I can Access these file by using "mysite.com/index.php/about" but I want to access as this "mysite.com/about" this not only for about page but all so very page that I try to access by using like this "mysite.com/contact-us"
.How do I fix this?
Write this code in your .htaccess file and in wamp server click on rewrite_module
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
IndexIgnore *
</IfModule>
By default, Codeigniter will call the about controller, unless a method is provided in the second uri segment, the index() method will be loaded.
If the index() method doesn't exist, it will 404.
The other way of doing it is to add a line in your application/config/routes.php file. Such as:
$route['about'] = 'main/about';
This means, a request where the url matches /about will map to main/about
Main being the controller and about being the method within that for example.
See this part of the documentation for a more detailed explaination
https://www.codeigniter.com/userguide3/general/routing.html
Hope this helps
Make sure that you have named the Class for your controller as the controllers file name. Then try to access the page as shown in the code below.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Pages extends CI_Controller {
function about()
{
$data['title'] = 'About us Page';
$this->load->view('about', $data); //about is in views
}
}
?>
The file name should be pages.php in this case.
To access the about page type in this link mysite.com/index.php?/controllerfolder/pages/about.
If you want clean url and remove index.php? edit your htaccess. Use this link to know how to edit your htaccess
https://www.formget.com/codeigniter-htaccess-remove-index-php/.
Hope this is helpful. Thanks

URI multilingual CodeIgniter

We're using Codeingiter library i18n (link) to create a multilingual site.
Before this, we had for example www.thedomain.com/register and register was a function in our controller. Now when we put this library it grabs the domain, the language string, the controller's name and the functions name: www.thedomain.com/es/homegf/register (where homegf is our controller).
We want this URI's to work without the name of our controller on it (www.thdomain.com/es/register) like in the librarie's examples but we think the problem is in our routes.php.
This is what we have in routes.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$route['default_controller'] = "homegf";
$route['404_override'] = '';
$route['^(en|es|de)/(.+)$'] = "$2";
$route['^(en|es|de)$'] = $route['default_controller'];
This is our .htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|files|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
You can find our code to review it at https://bitbucket.org/ticketcomunicacion/grinfood/src/17ddde60e340a1f2bc389f54ec579e1e903ee86b?at=multilenguaje
as i understand , you want the controller url without controller name .
if you want to change the URL from your controller name to other ,
in your routes.php write the following
$route['homegf/register'] = "register";
and you can access
domain.com/es/register
The problem was in our routes.
This is how we managed to make it work:
$route['^es/(.+)$'] = "homegf/$1";
$route['^en/(.+)$'] = "homegf/$1";
$route['^es$'] = $route['default_controller'];
$route['^en$'] = $route['default_controller'];
$route['(:any)'] = 'homegf/$1';

Url rewriting for codeigniter

How to rewrite particular url of website using .htaccess or CI routing
http://www.domain.com/user_controller/newfunction/username
as
http://www.domain.com/username
$route['/(:any)'] = "/user_controller/newfunction/$1";
or try htaccess
RewriteEngine On
RewriteRule ^([^/]*)$ /user_controller/newfunction/$1 [L]
/application/config/routes.php
$route['/all/other/routes/must/come/first!'] = "wherever/you/want";
$route['/(:any)'] = "/user_controller/newfunction/$1";
Its easier to use CI routing system found at application/config/routes.php
$route['(:any)'] = 'user_controller/newfunction/$1';
Don't forget to set your base url at application/config/config.php
$config['base_url'] = 'http://www.domain.com/';
Add remember to remove the index.php in the url. Edit your .htaccess file at the root of your site
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|_searches|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]
now in your user_controller, the function newfunction will look like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class user_controller extends CI_Controller {
public function __construct() {
parent::__construct();
// load things like models, libraries, helpers
}
function newFunction($slug)
{
echo 'hello my name is'.$slug;
}
}
when you visit http://www.domain.com/nash, your code should print out nash

codeigniter redirect from a view

I am building a site entirely on codeigniter .I have set my default controller as cuff.
so whenever users type the domain name it takes the control to that controller.
class Cuff extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function index()
{
$this->load->view('index');
}
public function navigate()
{
echo "test";
exit;
}
}
In my index view I want an anchor to navigate to a function in my default controller .
so when I write
our collection , it says page not found .
I have even set the base url like this
$config['base_url'] = "http://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= preg_replace('#/+$#','',dirname($_SERVER['SCRIPT_NAME'])).'/';
Cant seem to figure out the issue.
You missed the controller name
our collection
Set $config['base_url'] ='';
In your .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|js|css|favicon|robots\.txt)
RewriteRule ^(.*)$ /index.php?/$1 [L]
Make sure your controller is in controller's folder (not any subfolder) and is properly named cuff.php
Try navigating to http://yoursite/cuff/navigate
If you still see errors try rewriting the last line in .htaccess to:
RewriteRule ^(.*)$ /index.php/$1 [L]
(without question mark).
Please tell if that helps!
EDIT: and your links better all be in this form:
Link
The problem is in your routing.
Edit your routes.php (located in the folder ../application/config/) and add the code below:
$route['navigate'] = "Cuff/navigate";
For more information regarding Codeingiter URI Routing:
http://codeigniter.com/user_guide/general/routing.html
Hope this helped you.
Simply you use
"<?php echo site_url();?>/cuff/navigate">our collection</a>

Categories