Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter not working on localhost

I downloaded a website from ftp . The website is made with CodeIgniter. My problem is it doesn't seem to work on localhost.

Base URL in config.php:

$config['base_url'] = 'http://sample_localhost/mysite/';

Settings in database.php:

$db['default']['hostname'] = 'sample_localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'admin';
$db['default']['database'] = 'my_database';

Any help is very much appreciated. Thanks in advance!

like image 300
tara giska Avatar asked Jun 07 '13 09:06

tara giska


People also ask

Where is .htaccess file in CodeIgniter?

You should place your . htaccess file at your root directory not Inside the application folder.


2 Answers

1) Download the latest version of CodeIgniter.

2) Extract it and paste the extracted folder at the ‘htcdocs’ directory. In my scenario, I am using XAMPP 1.8.1, so I will paste it on the same directory. Also, you can rename the folder E.g. CI.

enter image description here

3) Take a look first at your config files and made some few modifications.

autoload.php

$autoload['libraries'] = array('database');
$autoload['helper'] = array('url');

config.php

$config['base_url'] = 'your localhost url';

in my case:

$config['base_url'] = 'http://localhost/CI/index.php/'; // your current URL on the address bar when displaying the welcome_message
$config['index_page'] = 'index.php'; // page where you want your viewers are redirected when they type in your website name

E.g. base_url — http://www.example.com/ index_page — index.php or straight ahead to news.php, it’s up to you

routes.php

$route['default_controller'] = 'site' // your controller's method, originally "welcome" to display welcome message

I set “site” as the default controller

database.php

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = '[your database]'; // e.g. CI_series
$db['default']['dbdriver'] = 'mysql';

TIP: Username as a default would be root if you don’t have any permissions for accessing the database yet. Also, leave password blank for now.

4) Start working with the Controllers Controllers are the heart of your application, as they determine how HTTP requests should be handled. A Controller is simply a class file that is named in a way that can be associated with a URI.

E.g.

http://www.example.com/index.php/blog/

In the above example, CodeIgniter would attempt to find a controller named blog.php and load it.

When a controller’s name matches the first segment of a URI, it will be loaded.

– Reference

Now, let’s type the code for our controller.

<?php
class Site extends CI_Controller
{
    function index()
    {
        $this->load->view('home.php');
    }
}
?>

Basically, this will just load our view/page called home

* What is load?

Loader, as the name suggests, is used to load elements. These elements can be libraries (classes) View files, Helpers, Models, or your own files. (ref)

This code snippet would let you display the page, home.php. Also since, you’re calling home.php, you must have this page under the views folder. Create your home.php, write anything you would like to display as a test for our first run and save it.

This code snippet would let you display the page, home.php. Also since, you’re calling home.php, you must have this page under the views folder. Create your home.php, write anything you would like to display as a test for our first run and save it.

home.php

<p>
My view has been loaded. Welcome!
</p>

Also, save our controller under the controllers folder, file name should be the same as your class name. In this case, it should be saved as site.php.

The first run:

enter image description here

like image 141
Gaurang P Avatar answered Sep 30 '22 07:09

Gaurang P


My be it should be like

$config['base_url'] = 'http://localhost/mysite/';

or like

$config['base_url'] = 'http://MY_IP/mysite/';

And check your index value in config file like

$config['index_page'] = 'index.php';

if it is so then you need to add index.php in your url before the controller name and check your folder has permissions also...and at your router.php see this

$route['default_controller'] = "welcome";

you can change if you want to display a controller by defaulty when you call sample_localhost/my_site

like image 40
Gautam3164 Avatar answered Sep 30 '22 08:09

Gautam3164