I am new into Codeigniter programming. I started CI by simply editing my default controller welcome. I added a function into the controller which is called on clicking a submit button. I havent done any modification in the default framework or in the config files. But upon clicking the submit button, the application returns a 404 response.
Not Found
The requested URL /login/welcome/main was not found on this server.
Apache/2.2.14 (Ubuntu) Server at localhost Port 80
The main function is what I wrote in the welcome controller. Can anyone please tell me what is wrong. My Codeigniter version is 2.1.0.
I re installed Apache and PHP. But no use still same. The code from controller and the welcome page below.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
error_reporting(E_ALL);
class Welcome extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* #see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$this->load->view('login_page');
//$this->load->view('phpinfo');
}
public function main()
{
$this->load->view('phpinfo');
}
And the welcome page is
<div id="container">
<h1>Ignitor</h1>
<div id="body">
<code>Please Login to continue. </code>
<div id="login_form">
<?php echo form_open(base_url().'welcome/main');?>
<ul>
<li>
<label>Username </label>
<?php echo form_input(array('id' => 'username', 'name' => 'Username'));?>
</li>
<li>
<label>Password </label>
<?php echo form_password(array('id' => 'password', 'name' => 'Password'));?>
</li>
<li> <?php echo form_submit(array('id'=>'submit'),'Let me in' )?></li>
</ul>
<?php echo form_close();?>
*/
Dude..
class Welcome extends CI_Controller {
public function index()
{
// echo 'Index';
$this->load->view('welcome');
// Then save the file in your application/views/welcome.php
// http://yours.com/index.php/welcome
// Or http://yours.com/welcome <-- With htaccess
}
public function main()
{
// echo 'Index';
$this->load->view('main');
// Then save the file in your application/views/main.php
// http://yours.com/index.php/welcome/main
// Or http://yours.com/welcome/main <-- With htaccess
}
}
By default, the index.php file will be included in your URL
You can easily remove this file by using a .htaccess file with some simple rules. Here is an example of such a file, using the "negative" method in which everything is redirected except the specified items:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Have you setup the .htaccess file? It should be something like:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
You are skipping index.php in the URL which means you are expecting a .htaccess file to be routing the request to CodeIgniters main index.php.
Use the URL /login/index.php/welcome/main to check if your .htaccess file is faulty or not present. If you renamed or moved the directory it is in to 'login' you may have forgotten to update that.
Your .htaccess in the 'login' directory should look something like -
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /login/index.php/$1 [L]
If it was finding the main index.php and the error was due to a view/model/controller naming issue you would get an 404 from CodeIgniter, not Apache.
Did you correctly name your welcomepage view file? In other words, your controller is asking to for login_page_view.php (or at least I believe that's the default filename convention) to be in the views folder based on the line of code:
$this->load->view('login_page');
Related
I've created one test.php file in location applications\views\test\test.php with controller in application\controller\Test.php & model in applications\models\Test_model.php.
In my routes.php file, I have added $route['test']='test/view'.
Please note there are some files present already which are working quite fine, one of which is login.php & it has value in routes.php as $route['login']='login/view'. Plus this file does have same model & controller files in likewise location.
When I try to access localhost\test, it gives me 404 error whereas for localhost\login works quite fine.
Can anybody help in routing? I'm new to codeigniter & I could not resolve this issue.
EDIT
htaccess works fine as localhost\login is loading properly along with other few files.
EDIT 2
Test_model.php
<?php
class Test_model extends CI_Model{
public function __construct()
{
$this->load->database();
}
}
?>
test.php
<div>Testing</div>
Test.php
<?php
class Test extends MY_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('test_model');
}
public function view()
{
$this->loadHeader();
$this->load->view('test/test');
$this->loadFooter();
}
}
?>
routes.php
$route['test'] = 'test/view';
EDIT 3
When I try to access localhost\application\controllers\Test.php, it gives me an error that says Class My_Controller not found. However, when I attempt to do it with any other file let's login with with the same location, it gives me the same error.
So I guess, it's able to find the controller because it's giving an error obviously but not able to load anything else.
Is there some sort of config file in which I have to mention every new page I create or something? There has to be something. This is pretty basic & I'm not able to get to the root of it.
EDIT 4
So this is what a problem is. When I copied my test view file to pages folder, it worked. Of course in controller I edited path of the file.
Now the real question is why didn't new folder named test under views work?
Finally, the culprit was this line in routes.php file:
$route['(:any)'] = 'pages/view/$1';
I was adding other route below this so it was getting default behaviour. I put them above this line & it worked like a charm. I never thought this could be the issue.
Line number matters, now.
Create a .htaccess file and paste this code below
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
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
I was using small letter for controller file name before,
however, recently move the file to other server that need to controller name to be start with capital
=========================================
Here is the rename example :
file name: Home.php
$route['default_controller']: "home";
link: http://example.com/index_folder/
==========================================
it show error of
Unable to load your default controller. Please make sure the controller specified in your Routes.php file is valid.
And when I change the route to
$route['default_controller']: "Home";
it still show the same error,
Only success if I go to
http://example.com/index_folder/Home/
How to fix that? Thanks for helping
Update
Here is the htaccess file, at the root of the project folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
The reason of rename is it seems the new server has problem if my Controller is in lower case
Linux is case sensitive, keep the file name and class name same and try again. It will work.
The same code will work fine on windows but give exception on Linux.
As per Documentation,
class Home extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
}
Then save the file to your application/controllers/ directory.
Important:-
The file must be called ‘Home.php’, with a capital ‘H’.
Now visit the your site using a URL similar to this:
example.com/index.php/home/
CodeIgniter can be told to load a default controller when a URI is not present, as will be the case when only your site root URL is requested.
To specify a default controller, open your application/config/routes.php file and set this variable:
$route['default_controller'] = 'home';
Where ‘home’ is the name of the controller class you want used.
If you now load your main index.php file without specifying any URI segments you’ll see your “Hello World” message by default.
I'm using CodeIgniter. I set my default controller is 'cart'. And i add one more file in Controller folder that name is admin.php. admin.php having following code,
<?php
class Admin extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
echo 'Hello World!';
}
}
?>
If i give the url like http://lo...host/codeigniter/admin means, It shows 404 Not Found. If i give http://lo...host/codeigniter/, It shows cart page as good.
And if i change default controller in routes.php as 'admin', http://lo...host/codeigniter/ shows 'Hello World!'
Seem like your are not removing index.php from your URLs. Try to access to this URL:
http://lo...host/codeigniter/index.php/admin
If it works, then add this to your .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
More info:
http://ellislab.com/codeigniter/user-guide/general/urls.html
Ok I have adopted a CodeIgniter Setup,
My route.php in config looks like this:
$route['default_controller'] = "development";
My development.php controller looks like this:
class Development extends Controller
{
function Development() {
parent::Controller();
}
public function Index() {
$this->load->view('index');
}
function show() {
$this->load->view('show');
}
}
When I go to the root folder, in my browser, it does load the index.php view, I want to make a link to show.php which is also in my Views dir. the URL I'm using is eg: my.server/test/codeigniter/ but when I go to my.server/test/codeigniter/show my show.php doesn't load. Am I doing this correctly?
I should mention I've tried public function show() also and it doesn't work, also I have no .htaccess file in the directory
Any advice would help!
Two rules are enough in your .htaccess file:
# check if the requested resource is not an existing file
RewriteCond %{REQUEST_FILENAME} !-f
# rewrite internally to index.php
RewriteRule ^(.*)$ index.php [QSA,L]
Mod_rewrite and AllowOverride All assumed enabled.
Ken Struys comment on your question is correct. The default controller status you give a controller means that controller and the index function will load if you go to
my.server/test.
Other than that, you need to include index.php/controller_name/function_name to get to your controller functions.
Unless that is, you implement some fancy .htaccess url rewriting, which I can't help you with.
You can set it in routes.php as below:
$route['show']='development/show';