Xander
Xander

Reputation: 1715

codeigniter controller function issue

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();?>

    */ 

Upvotes: 0

Views: 3384

Answers (4)

Dan Brown
Dan Brown

Reputation: 111

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.

Upvotes: 1

W Kristianto
W Kristianto

Reputation: 9303

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]

Upvotes: 1

sikander
sikander

Reputation: 2286

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]

Upvotes: 1

Stephane Grenier
Stephane Grenier

Reputation: 15927

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');

Upvotes: 0

Related Questions