Codeignite hello world code in docs doent work - php

I just started using CodeIgniter and I'm stuck at the hello world code in the docs. Each time I type in the name of the blog controller in the url bar and hit enter, I get this error:
404 Error Page Not Found!
localhost/php/CodeIgniter/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
route.php
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
config.php
$config['base_url'] = 'http://localhost/php/CodeIgniter/';
$config['index_page'] = 'index.php';
controllers/Blog.php
<?php
class Blog extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
}
How Can I Resolve This?

You have to set a URL string on [route.php] corresponding controller class/method.
$route['default_controller'] = 'blog/index';
$route['blog/(:any)'] = 'blog/show/$1'
You can leave the URL "http://localhost/" referencing "/" as home by default_controller on route.php
$config['base_url'] = 'http://localhost';
Please browse more about .htaccess and friendly URL. If you get 404 error maybe your URL is not friendly.
Friendly:
http://localhost/blog/lorem-ipsum
Not Friendly:
http://localhost/index.php/blog/lorem-ipsum
Documentation: https://www.codeigniter.com/userguide3/general/routing.html

Related

How can I remove method name from URL in codeigniter?

I am using the multilingual feature in my Codeigniter website.
current URL looks like
http://example.com/hi/news/view/news-title
I want this like
http://example.com/hi/news/news-title
current routes.php is looking like
$route['default_controller'] = 'home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
//Routes for multilingual url
$route['^hi/(.+)$'] = "$1";
$route['^en/(.+)$'] = "$1";
// '/en' -> use default controller
$route['^hi$'] = $route['default_controller'];
$route['^en$'] = $route['default_controller'];
.htaccess file looks like
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|jquery|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]
I tried these but it not works and always give me 404 error
$route['^(en|hi)/news/(:any)'] = "news/view/$1";
$route['hi/news/(:any)'] = 'news/view/$1';
You have not specified the name of the controller or the controller called "News" is not exists.
Specify the name of the controller that is having the "news" method e.g.
$route['(en|hi)/news/(:any)'] = "home/news/view/$1";
OR
Create a "News" controller and create a "view" method e.g.
class News extends CI_Controller {
public function view()
{
}
}

seo frienldy url is using cyrillic х character and it is always getting 404. How to fix it?

I have Seo friendly URL problem in my codeigniter web.
$route['(:any)'] = 'main/change_route/$1';
here is routes.php in a config folder. Any request must to call change_route method of the main class.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class main extends CI_Controller {
public function change_route($url='')
{
die(urldecode($url));
}
}
This is my controller which is showing just request URL.
So my problem is here.
http://localhost/safety/black-shoes-ар-гутал
Above url is working correct and displaying that "black-shoes-ар-гутал"
But http://localhost/safety/black-shoes-хар-гутал this url is getting always 404. If i will remove cyrillic 'х' character from this URL so it will work correctly.
How can i fix it ?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
It is my .htaccess .
In routes.php file, use change
$route['translate_uri_dashes'] = FALSE
this to
$route['translate_uri_dashes'] = TRUE;
and also in your config.php file change
$config['uri_protocol'] = 'REQUEST_URI';
to
$config['uri_protocol'] = 'PATH_INFO';
the uri will always be url-decoded
and if you want to remove controller name from your url use this
$route['^(method1 | method2 | method3)(/:any)?$'] = "controllerName/$0";
if your methods having underscore like
"function method_name(){
#yourcode
}"
than use this
$route['method-name'] ='controllerName/method_name';
if I hope all of my above example will help you

CodeIgniter setting up routing properly

I'm trying to setting up my codeigniter routing to make an authentication form for an admin panel, but the routing is not working. I'm a beginner with CodeIgniter and I think I'm missing something.
In my server I put the CodeIgniter fiels inside folder in my root directory, so it can be accessed like this:
/localhost/ci
In my config.php I set my base url and remove the index page to remove index.php:
$config['base_url'] = 'http://localhost/ci/';
$config['index_page'] = '';
Also I have edited the .htaccess to remove the index.php like CodeIgniter documentation says, so it looks like this:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Now, my routes config file looks like this. I set my default page and a route to my Auth controller:
$route['default_controller'] = 'auth';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['auth/login'] = "auth/login";
This is my Controller:
class Auth extends CI_Controller {
function __construct() {
parent::__construct();
// load libraries
}
function index() {
if (! $this->ion_auth->logged_in()) {
// redirect them to the dashboard page
redirect(base_url('auth/login'), 'refresh');
} else {
// show the dashboard page
redirect(base_url('dashboard'), 'refresh');
}
}
function login() {
$this->load->view('login');
}
When I enter in the web-browser http://localhost/ci/ it redirects me to http://localhost/ci/auth/login, but then a 404 Not Found error ir raised. What am I missing? I'm a bit confused at this point, thanks.
Try changing your .htaccess from:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
To
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /ci/index.php/$1 [L]

Codeigniter Routing Issues on AWS EC2

I have been searching the stackoverflow all day for an answer to this and I just can't seem to find one.
I have my Codeigniter app on an AWS EC2 server.
Here is my routes.php:
$route['default_controller'] = 'pages/home';
$route['(:any)'] = 'pages/$1';
EDIT Here is my current routes.php:
$route['default_controller'] = 'pages/home';
$route['about'] = 'pages/about';
$route['contact'] = 'pages/contact';
$route['home'] = 'pages/home';
Here is my Pages.php:
class Pages extends CI_Controller {
function __construct(){
parent::__construct();
}
public function home(){
$this->view('home');
}
public function about(){
$this->view('about');
}
public function contact(){
$this->view('contact');
}
public function view($page)
{
if (!file_exists(APPPATH.'/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
Here is my .htaccess:
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]
My config.php:
$config['index_page'] = '';
PROBLEM
If I go to (my-url), it uses my default_controller.
If I go to (my-url)/home or /about or /contact, I get a 404 error.
If I go to (my-url)/index.php/home or /about or /contact, I get the default_controller.
I have double checked that mod_rewrite is installed and enabled. I can't seem to figure out why I'm not getting anything. I followed the tutorial and read a bunch of stackoverflow questions and answers to try to solve it, but to no avail.
Please help.
You can not use routes as like that:
$route['(:any)'] = 'pages/$1';
It will not work with all functions as you need.
Just Suggestion:
Use your routes as:
$route['about'] = 'pages/about';
$route['contact'] = 'pages/contact';
$route['view/(:any)'] = 'pages/view/$1';
......
You need different routes for different URL if you use (:any) as first param it will not work properly for all functions.
Try this
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]
Note when using APPPATH constant, the path ends with '/' already
try change
if (!file_exists(APPPATH.'/views/pages/'.$page.'.php'))
to
if (!file_exists(APPPATH.'views/pages/'.$page.'.php'))

Only welcome controller loading in codeigniter when hosted under cpanel

The app which i'm creating using codeigniter is loading on my localhost. But when i uploaded the same into cpanel its showing "404 Page Not Found".
I'm able to access default welcome controller other than this it shows 404 page not found.
Below are my configuration in application/config/config.php
$config['base_url'] = '';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
.htaccess configurations are as follows
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
configuration in application/config/routes.php are as below
$route['default_controller'] = 'login';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
If I change the default_controller to welcome then welcome is loading fine.
--Thanks in advance
As said in the comments : Your controller's file name must start with an uppercase.
See http://codeigniter.com/userguide3/changelog.html
Changed filenaming convention (class file names now must be Ucfirst
and everything else in lowercase).
:-)

Categories