I'm new to PHP and CodeIgniter, and I'm having trouble setting up the routing of static pages in my CodeIgniter app.
I have the ciblog/pages/views/about and it works.
I want to change it into ciblog/about.
I already have in the routes.php the $route['(:any)'] = 'pages/view/$1';
and it doesn't work it says in the browser:
page not found..
Please please help me I want to learn how to set up a static page....
Here is the code in the routes.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['default_controller'] = 'welcome';
$route['(:any)'] = 'pages/view/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
?>
This is in my controller
<?php
class Pages extends CI_Controller{
public function views($page = 'home'){
if(!file_exists(APPPATH.'views/pages/'.$page.'.php')){
show_404();
}
$data['title'] = ucfirst($page);
$this->load->view('templates/header');
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer');
}
}
?>
here is in the .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
It will work, If your Controller is along the lines of...
This is the simplest, bare bones option.
class Pages extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->load->view('welcome_message');
}
public function view($page) {
$this->load->view($page);
}
}
Where the view method accepts a parameter.
Of course you would need to check that the view file exists etc and take the appropriate action.
Update:
Now that you have shown your Controller, I can see you are referring to view in your routes BUT you Controller method is called views ( with an added s).
They have to be the same.
So the quickest way to make them the same is to change
$route['(:any)'] = 'pages/view/$1';
to ( By adding an s to views so its the same as the controller method )
$route['(:any)'] = 'pages/views/$1';
OR you can simply change the method name from views to view.
Your choice!
Assuming the "about" page is to be served by the view method of the pages controller,
$route['about'] = 'pages/view/about';
Related
I have a system on the production server which I want to emulate in local to update it but the previous programmer have use a strange architecture. There is a codeigniter architecture and another one inside the first...
application
admin (which content controller and models... probably unused)
common (language and models...)
controllers
js (why here...)
kanri
cache, config, controllers, core, helpers, models, views, ...
user
cache, config, controllers, core, helpers, models, views, ...
system
etc...
I had a lot of fun to emulate this on my local machine because all path are absolute, yeepee.
So currently, it's on my localhost, correctly configurated but when I try to login, the page just reload. I found out the method login of the controller login (so kanri/login/login) is called but always routed to kanri/login/index.
On the index page, there is only a form which redirect to the kanri/login/login method, so even the simple redirection do not work.
The file kanri/route.php :
defined('BASEPATH') OR exit('No direct script access allowed');
$route['default_controller'] = 'login';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
The main .htaccess :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^kanri(/(.*))?$ kanri.php?/$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user(/(.*))?$ user.php?/$2 [L]
</IfModule>
MY_Controller :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
if($this->session->userdata('flag_admin_login')===FALSE || empty($this->session->userdata('flag_admin_login'))){
//print "***".$this->session->userdata('member_id')."!!!!!";
redirect('/', 'refresh');
}
}
}
My Login controller :
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
echo $this->router->fetch_class() . "<br/>"; // display the controller
echo $this->router->fetch_method() . "<br/>"; //display the method
}
public function index(){
//all code commented
}
public function login(){
//all code commented
}
}
I have a lot to work to do on it and I'm still stuck on the running...
Thanks for your time!
The solution was quite simple...
$config['uri_protocol'] = 'REQUEST_URI';
$config['enable_query_strings'] = FALSE;
I'm getting a 404 error on my CodeIgniter site, I've had a look around at the other answers and playing with the .htaccess but yet to solve. Its located on my domain.co.uk/admin
Controller -
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Projects extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index() {
$this->load->model('project');
$data['projects']=$this->project->get_projects();
echo"<pre>";print_r($data['projects']);echo"</pre>";
}
}
Model -
<?php
class Project extends CI_Model{
function get_projects($num=10,$start=0){
$this->$db->select()->from('projects')->where('active',1)->order_by('date_added','desc')->limit($num,$start);
$query=$this->db->get();
return $query->result_array();
}
}
I've also added these routes -
$route['default_controller'] = 'projects';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
Currently have this .htaccess in my domain/admin folder
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
Can anyone see why I might be getting a 404 error
What do you have on your $route['default_controller'] variable? (It shoulds be defined in \application\config).
If you have $route['default_controller'] = "Projects", your index URL should be domain/admin/index.php/projects or domain/admin/projects*
*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]
$config['index_page'] = '';
On codeigniter 3 you should always have your first letter of your class and file name as upper case on models and controllers and libraries.
Also I would rename projects model to Model_project.php because codeigniter may get confused if have same names.
Model Model_project.php
class Model_project extends CI_Model {
}
Controller Project.php
class Project extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('model_project');
}
}
You may need to set up your routes
http://www.codeigniter.com/user_guide/general/routing.html
If you are using a codeigniter 3.0 then update your MODEL and CONTROLLER starting with capital letters and also don't give a same name for both model and controller as you have given in the above code.
For example:
class Projects extends CI_Controller {
//code comes here
}
In this Projects the name is absolutely fine but mark it that you file name should also start with capital letter that is Project.php
Another for model:
class Projects_model extends CI_Model {
//code comes here
}
This is the way to make a proper way to work with codeigniter and also i have already told you that the MODEL and CONTROLLER filename must be started with the capital letter.
Thank You.. hope it will help you
The short: The code sample below does not echo "Here I am!" It will not echo from the _construct function. It will echo if I move the echo statement above the class so I know CI is reaching this controller.
The backstory:
I've inherited a project done in CodeIgniter 2.1.2. I'm supposed to replicate an application to another directory with a different subdomain and pointing the database config to a different database. I have the database updated in the config. I've updated the base bath. I've set environment to development so I can get error messages. There are no errors.
if (!defined('BASEPATH'))
exit('No direct script access allowed');
if (!ini_get('date.timezone')) {
date_default_timezone_set('my-time-zone');
}
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('mod_login');
$this->load->helper('date');
}
function index() {
echo "Here I am!"; //Nothing echos
}
function logout{
//logout function here.
}
}
Any CodeIgniter fans here have an idea why?
Your controller should be like this.
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Login extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('mod_login');
$this->load->helper('date');
}
public function index()
{
echo "Here I am!";
}
}
and controller name should be loging.php
and in config/routes.php
$route['default_controller'] = "login";/set default conntoller
Extra Notes: (to remove index.php in URL)
in config/config.php
$config['base_url'] = '';
$config['index_page'] = '';
and .htaccess (place outside application folder)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Class name of controller must be uppercase. From documentation:
Note: Class names must start with an uppercase letter.
In other words, this is valid:
<?php
class Blog extends CI_Controller {
}
?>
This is not valid:
<?php
class blog extends CI_Controller {
}
?>
More: https://ellislab.com/codeigniter/user-guide/general/controllers.html#hello
I have successfully run the test webpage in my local machine and it works! But when I uploaded it to the production server (iPage), I got this error:
An Error Was Encountered
Unable to determine what should be displayed. A default route has not been specified in the routing file.
I have here the .htacess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|assets|img|bg|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
Outside the application folder.
and in my routes.php:
$route['default_controller'] = 'login';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
and in my config.php:
$config['base_url'] = 'http://example.com/System';
$config['uri_protocol'] = 'REQUEST_URI';
$config['enable_query_strings'] = TRUE;
I have login.php inside controller folder with the ff codes:
defined('BASEPATH') OR exit('No direct script access allowed');
class login extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('login_model');
/* enable session */
$this->load->library('session');
}
public function index()
{
if ( ! file_exists(APPPATH.'/views/index.php'))
{
/* Whoops, we don't have a page for that! */
show_404();
}
$this->load->view('index');
$this->load->view('templates/footer');
}
Am I missing or did something wrong?
CodeIgniter 3 requires that your classes are named in a Ucfirst manner and the filenames must match the class names.
Therefore, you need to rename your 'login.php' to 'Login.php', as well as change the class declaration to class Login extends CI_Controller.
Your routes contain
$route['default_controller'] = 'login';
So in controller page name should be login.php
Inside login.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller//check this
{
public function __construct()
{
parent::__construct();
}
Brief: check this class Login extends CI_Controller
And base_url should be
$config['base_url'] = '';
I started with CodeIgniter today, and I’m following the beginner tutorial from phpacademy.
Right now I got an odd problem, I got the following very simple controller:
<?php if ( ! defined('BASEPATH')) exit("No direct script access allowed");
class Site extends CI_Controller {
public function index()
{
}
public function home()
{
$this->load->view("view_home", $data);
}
function about()
{
$data['title'] = "About!";
$this->load->view("view_about", $data);
}
function test()
{
echo "test";
}
}
It always loads the index function fine. When I test it, it echoes whatever I want it to echo. But when I call the other functions nothing happen.
I didn’t set up my .htacces yet, but when I visit the following address:
localhost:8899/index.php/site/home
it doesn’t load the home function. same goes for the other function (“about” and “test”);
When I call one of the functions (“home”, “about” and “test”) from within the index() function it does call it how it should:
class Site extends CI_Controller {
public function index()
{
$this->home();
}
public function home()
{
$this->load->view("view_home", $data);
}
I’m not sure what is going wrong here. Hopefully, someone can guide me in the right direction!
My Routes:
<?php if ( ! defined('BASEPATH')) exit("No direct script access allowed");
$route['default_controller'] = "site";
$route['404_override'] = '';
If somebody came across this since no solutions above worked, ok you can try these steps where i got the thing worked (local with xampp):
Change the base_url in config.php into http://localhost/application-name/
Empty the index_page (the default value should be index.php)
Go to root folder of your application and check that index.php is there
Make .htaccess file in there and write into that these:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
i would make sure the controller class has a constructor to load the base codeigniter base classes.
function __construct()
{
parent::__construct();
}
in each controller class tells your class to use the code igniter system. if this is not present it won't know to how to load these functions.
Just to be clear, make sure it calls the other methods by url/site/about etc...
Also, have you got php errors displaying on you development server?