0

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

    */ 

2
  • 1
    you have to placed the form action like this . <?php echo base_url();?> login/welcome/main/ AND you have to set the base_url in your config file Commented Mar 22, 2012 at 4:49
  • I am sorry. I didn't understand you completely. I have set-up the base_url() function correctly. What is it regarding placing it here..? Thanks for replying Commented Mar 22, 2012 at 5:38

4 Answers 4

1

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]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for such a detailed reply. This is what I have done in the application. I have all the view files in the view folder. The phpinfo i mentioned is a file I have in my view page as phpinfo.php. At least en echo would work, with or without the index.php. But nothing happens except the first page in the index function is displayed. The real fact is that, even if i dont mentions the function name in the url, the controller should point into the index function, which is not happening. :(
If you have a folder to run your app, eg : yours.com/folder/welcome/main, u can put htaccess in folder
1

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]

2 Comments

Make that last line "RewriteRule ^(.*)$ /login/index.php/$1 [L]" as he appears to not be running CI from the server root.
I dint have any .htaccess file in the framework by default. I dont know how to set it up. Will copying this code into that file work?
1

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.

3 Comments

Thanks,Dan. This time it really worked. Thanks a lot. /login/index.php/welcome/main worked. Even though I haven't understood the issue really, I am glad that it worked. Can you please explain a little more?
CodeIgnitor URLs (and other Frameworks) are split into two halves. The real path and file that exists on the server ('/login/index.php') and the route+parameters ('/welcome/main'). index.php then processes the route. The .htaccess file is optional and just tells Apache to internally redirect any URL that includes the directory it is in to that index.php file so that 'index.php' can be left out of the URL completely. The RewiteCond line lists exceptions that won't be redirected.
Also, if you add any other directories off your CodeIgniter directory that your HTML references such as 'stylesheets' or 'scripts' you will want to add them to that list of exceptions to access them as real files rather than letting Apache pass them to CodeIgnitor.
0

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

1 Comment

The default name was welcome_page.php which I renamed into this login_page.php. The same data was moved into another computer where it worked with no issues. The controller and the function , everything was working properly. Thanks for the reply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.