I’m new with codeigniter, usually i just write native php code but today i got one request project from someone that need to use codeigniter. but, a problem just occur when i start code with CI (codeigniter).
i am usually use, nginx - php - mysql and i have read CI user_guide properly and i just understand how to use that framework. ok, let’s face the problem:
I’ve create a controller just like this:
<?php
class post extends CI_Controller
{
public function __construct() {
parent::__construct();
// load site_model
$this->load->model('site_model');
}
public function index()
{
$data['posts'] = $this->Site_model->getPosts();
$data['title'] = 'Index Page!';
// load view
$this->load->view('template/header', $data);
$this->load->view('post/index', $data);
$this->load->view('template/footer');
}
public function view($id)
{
$data['post'] = $this->Site_model->getPosts($id);
/*
* show 404 page error if the requested ID
* is not found in the database.
*/
if (empty($data['post']))
{
show_404();
}
$data['title'] = $data['post']['post_title'];
// load the needed view
$this->load->view('template/header', $data);
$this->load->view('post/view', $data);
$this->load->view('template/footer');
}
}
i think this is very basic CI code, but when try to access this url: http://localhost/cif/post and the server just show welcome to nginx page
fyi, http://localhost/cif is my base project and i was set permission both of dir and files properly
this is my nginx config (/etc/nginx/sites-available/default):
# You may add here your
# statements for each of your virtual hosts to this file
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
server {
listen 80; ## listen for ipv4; this line is default and implied
listen [::]:80 default ipv6only=on; ## listen for ipv6
root /var/www;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.html;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
# Only for nginx-naxsi : process denied requests
#location /RequestDenied {
# For example, return an error code
#return 418;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \\.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\\.ht {
deny all;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# root html;
# index index.html index.htm;
#
# location / {
# try_files $uri $uri/ /index.html;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
#
# root html;
# index index.html index.htm;
#
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
#
# ssl_session_timeout 5m;
#
# ssl_protocols SSLv3 TLSv1;
# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
# ssl_prefer_server_ciphers on;
#
# location / {
# try_files $uri $uri/ /index.html;
# }
#}
To use CodeIgniter it is advisable to make sure that the initial installation is correct and the ‘Welcome to CodeIgniter’ default view is appearing with no errors.
Once that is running then investigate:
config/config.php and play about with the log file settings. $config[‘log_threshold’] = 0; // 1,2,3, 4; and check differences in log file output
config/config.php removal of index.php along with .htaccess file settings
This is your modified file which I have tested locally using:
<?php
class Post extends CI_Controller
{
public function __construct()
{
parent::__construct();
// load site_model
#$this->load->model('site_model'); // unremm once "Welcome to CodeIgniter" is working
}
public function index()
{
$view_file = 'welcome_message.php';
if(file_exists( VIEWPATH .$view_file ) )
{
$this->load->view($view_file);
}
else
{
echo 'Missing file: ' .$view_file;
}
}#
public function OLD_index()
{
$this->load->model('site_model');
$data['posts'] = $this->Site_model->getPosts();
$data['title'] = 'Index Page!';
// load view
$this->load->view('template/header', $data);
$this->load->view('post/index', $data);
$this->load->view('template/footer');
}#
public function view($id)
{
$data['post'] = $this->Site_model->getPosts($id);
/*
* show 404 page error if the requested ID
* is not found in the database.
*/
if (empty($data['post']))
{
show_404();
}
$data['title'] = $data['post']['post_title'];
// load the needed view
$this->load->view('template/header', $data);
$this->load->view('post/view', $data);
$this->load->view('template/footer');
}#
}
Change the first server section to this and then restart NGiNX:
server {
listen 80; ## listen for ipv4; this line is default and implied
listen [::]:80 default ipv6only=on; ## listen for ipv6
root /var/www;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.html;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
location /cif/ {
try_files $uri $uri/ /cif/index.php$uri?$query_string;
}
# Only for nginx-naxsi : process denied requests
#location /RequestDenied {
# For example, return an error code
#return 418;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \\.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\\.ht {
deny all;
}
}
From the error it looks like there is a loop and index.php is being called repeatedly.
Ensire your error reporting is set to 4 and run the default “Welcome to CodeIgniter”.
Check your error_log and it should be similar to the results from my error log below.
Next, delete the error log and run your http://localhost/index.php/post then check your error log again.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>
DEBUG - 18:53:02 --> Config Class Initialized
DEBUG - 18:53:02 --> Hooks Class Initialized
DEBUG - 18:53:02 --> Utf8 Class Initialized
DEBUG - 18:53:02 --> UTF-8 Support Enabled
DEBUG - 18:53:02 --> URI Class Initialized
DEBUG - 18:53:02 --> Router Class Initialized
DEBUG - 18:53:02 --> Output Class Initialized
DEBUG - 18:53:02 --> Security Class Initialized
DEBUG - 18:53:02 --> Input Class Initialized
DEBUG - 18:53:02 --> Global POST and COOKIE data sanitized
DEBUG - 18:53:02 --> Language Class Initialized
DEBUG - 18:53:02 --> Loader Class Initialized
DEBUG - 18:53:02 --> Config file loaded: ci_test/config/config_ci_jokes.php
DEBUG - 18:53:02 --> Config file loaded: ci_test/config/config_max_sess.php
DEBUG - 18:53:02 --> Helper loaded: url_helper
DEBUG - 18:53:02 --> Helper loaded: string_helper
DEBUG - 18:53:02 --> Helper loaded: inflector_helper
DEBUG - 18:53:02 --> Helper loaded: html_helper
DEBUG - 18:53:02 --> Helper loaded: text_helper
DEBUG - 18:53:02 --> Helper loaded: form_helper
DEBUG - 18:53:02 --> Database Driver Class Initialized
DEBUG - 18:53:02 --> User Agent Class Initialized
DEBUG - 18:53:02 --> Model Class Initialized
DEBUG - 18:53:02 --> Model Class Initialized
DEBUG - 18:53:02 --> Controller Class Initialized
DEBUG - 18:53:02 --> File loaded: ci_test/views/welcome_message.php
DEBUG - 18:53:02 --> Final output sent to browser
DEBUG - 18:53:02 --> Total execution time: 0.1855
You could also try inserting the following into your post.php
controllers/post.php
<?php
// DEBUG - HALT TO ENSURE THIS CLASS IS CALLED
echo __FILE__;die
class Post extends CI_Controller
{
public function __construct()
{
parent::__construct();
// load site_model
#$this->load->model('site_model');
}