I'm trying to do my site like this tutorial http://www.codeigniter.com/user_guide/tutorial/static_pages.html
but have some problem with routing. I have page "createBook" for default and when I call localhost it's work! But when I do like localhost/createBook I have Error 404. What I'm doing wrong?
In my controller:
public function view($page = 'createBook')
{
if ( ! file_exists(APPPATH.'views/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$this->load->view($page);
}
routes.php file
$route['default_controller'] = 'Books/view';
$route['(:any)'] = 'Books/view/$1';
And I have view files in my views folder named Success and createBook
I don't really get your question but it seems to me that you are trying to call a controller named createBook from the url, but in your function it shows that if there is no views/createBook.php than show 404, maybe its why you get a 404, because your calling a controller as a view, i think your function should be like this :
public function view($page = 'createBook')
{
if ( ! file_exists(APPPATH.'controllers/' . $page . '.php'))
{
// Whoops, we don't have a page for that!
$this->output->set_status_header('404');
show_404();
}
$this->load->view($page);
}
If you want your url to look something like this :
example.com/view/book/3
Your controller should look like this :
class View extends CI_Controller
{
public function book($book_id)
{
//Search the database for records about a book with its id, and return the data
//and assign it to a variable(in this case $data) ex : $data["book-name"] = ...
//And on your view you can call these values using ex : echo $book-name
$this->load->view('books_view', $data);
}
}
And make sure your .htaccess file next to the index.php looks like something like this :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
Then go to your config, and change this line to :
$config['index_page'] = '';
Now you can access all of your controllers this way :
http://www.example.com/controller_name
Related
I am unable to redirect to page using the redirect() in codeigniter. Also the same problem if I try to use location.href within view. It just redirects me to the page without the css and js includes
mY CONFIG
$config['base_url'] = 'http://localhost/tsb/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI'; //I have switched the 3 protocols too
HTACCESS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
BOOK CONTROLLER
public function psg_sel()
{
$book_name = $this->input->post('book_name');
$book_id = $this->cj_model->get_book_id($book_name);
$ch_id = $this->input->post('chapter_id');
$cj_mask = $this->input->post('cj_mask');
if($cj_mask == ''){
$psg_sel = $this->cj_model->psg_sel($book_id, $ch_id);
if($psg_sel === true){
redirect(base_url() . 'passage/', 'refresh');
}
}
}
PASSAGE CONTROLLER
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Passage extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->database();
}
public function index()
{
$data['page_name'] = 'passage';
$this->load->view('pages/index', $data);
}
}
Please help I dont know whats going wrong. I can access http://localhost/tsb/passage/ directly from address bar but none of these will work properly location.href="passage/";or redirect(base_url() . 'passage/', 'refresh'); This is how it displays
At controller try this code. First load url helper then try..
The redirect statement in code igniter sends the user to the specified web page using a redirect header statement.This statement resides in the URL helper which is loaded in the following way:
$this->load->helper('url'); //loads url helper
Controller:
public function psg_sel()
{
$this->load->helper('url'); //loads url helper
$book_name = $this->input->post('book_name');
$book_id = $this->cj_model->get_book_id($book_name);
$ch_id = $this->input->post('chapter_id');
$cj_mask = $this->input->post('cj_mask');
if($cj_mask == ''){
$psg_sel = $this->cj_model->psg_sel($book_id, $ch_id);
if($psg_sel === true){
redirect(base_url('passage'), 'refresh');
}
}
}
After searching for hours I still don't get why I get a 404 error when I do a form_submit.
My controller looks like this:
class pages extends CI_Controller{
function view($page = 'home'){
$this->load->helper('url');
if(!file_exists('application/views/pages/'.$page.'.php')){
show_404();
}
$data['title'] = $page;
$this->load->view('templates/view',$data);
}
public function login_validation(){
}
The view page:
<?php echo form_open('pages/login_validation');
echo form_input(array(
'name' => 'firstname',
'placeholder' => 'Voornaam',
'class' => 'form-control input-lg',
'tapindex' => '1'
));
echo form_submit(array(
'name' => 'login_submit',
'value' => 'Register',
'class' => 'btn btn-primary'
));?>
.htaccess (in the root folder):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|indexcp\.php?|resources|robots\.txt)
RewriteRule ^([A-Za-z0-9_/.-]+)$ index.php?/$1
I have this line in the autoload.php:
$autoload['helper'] = array('form','url');
In config.php:
$config['base_url'] = 'http://localhost/';
$config['index_page'] = '';
routes.php:
$route['(:any)'] = "pages/view/$1";
$route['default_controller'] = "pages/view";
$route['404_override'] = '';
What is wrong with this configuration?
The problem you are having is with the following route (I am assuming you've posted the whole of your routes file):
$route['(:any)'] = "pages/view/$1";
This means that any file you load is going to go to the pages controller, call the function view, and pass in the parameter which is as $1. In this case, it's going to pass through either 'pages/login_validation' or 'login_validation' (I'm not 100% sure which, and I can't test at the moment.
Your view function is checking to see if the file 'application/view/pages/login_validation.php' or 'application/view/pages/pages/login_validation.php' exists, and when it can't find it, it is giving the 404 page.
To fix this, you can add the following into the routes.php file (above the $route[(:any)] = 'pages/view/$1'; line):
$route['pages/login_validation'] = 'pages/login_validation';
That will explicitly check that the page wanting to be loaded is the login_validation one, and then call that function within the pages controller.
If you don't want to keep adding routes each time, then you could modify the view function in pages to check if the $page variable matches the name of a function, and if so then call that function:
if (function_exists($page))
{
$page();
}
The trouble you might have then is that you won't easily be able to pass through additional parameters to the given page. You might also accidentally name a page the same as a function in PHP, and then it will try and call that instead of loading the page you intend.
I have a login system that works, but on the redirect function it gives me the error The requested URL "http://localhost/musiclear/index.php/welcome" cannot be found or is not available. Please check the spelling or try again later.
This is where I am using it (login.php):
function validate_credentials() {
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if ($query) { // if users credentials validated
$data = array('usernames' => $this->input->post('username'),
'is_logged_in' => true);
$this->session->set_userdata($data); //set session data
redirect('welcome', 'refresh'); //redirect to home page
} else { //incorrect username or password
$this->index();
}
}
This is where I am directing it to (welcome.php):
class Welcome extends CI_Controller {
public function index() {
$this->home();
}
public function home() {
$this->load->model('model_users');
$data['title'] = 'MVC Cool Title'; // $title
$data['page_header'] = 'Intro to MVC Design';
$data['firstnames'] = $this->model_users->getFirstNames();
// just stored the array of objects into $data['firstnames] it will be accessible in the views as $firstnames
$data['users'] = $this->model_users->getUsers();
$this->load->view('home_view', $data);
}
}
Im thinking it is something wrong with the path, or where its linking to but im not sure. This is my directory setup:
Can someone please tell me whats wrong and how I can make it link to my page? Thanks so much
Have you created the .htaccess in your application folder?
Maybe this can work for your project:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /musiclear/index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /musiclear/index.php
</IfModule>
You are welcome
I have controller welcome below that redirect to function of another controller in controllers/auth/login.php
function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->library('tank_auth');
}
function index() {
if (!$this->tank_auth->is_logged_in()) {
redirect('/auth/login');
} else {
$data['user_id'] = $this->tank_auth->get_user_id();
$data['username'] = $this->tank_auth->get_username();
$this->load->view('welcome', $data);
}
}
Here, the config.php:
$config['base_url'] = '';
$config['index_page'] = 'index.php';
it work well. But when i specified the base_url in config file into:
$config['base_url'] = 'http://localhost/cilog/';
$config['index_page'] = '';
Object not found. why it be? but it work again when i specified index_page into index.php.
I believe this is because of the way CodeIgniter handles it's URL's. I'm not actually sure why Codeigniter does this, but they include index.php in there URL.
So your URL would look something like http://localhost/cilog/index.php/auth/login
You can either rewrite your .htaccess file to remove the index.php by putting this in:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
and keep your $config['base_url'] set too "http://localhost/cilog/"
OR
specify both a $config['base_url'] and $config['index_page']
See here for more info: http://ellislab.com/codeigniter/user-guide/general/urls.html (Removing the index.php file)
So this is my first Codeigniter project, and I'm about to commit seppuku.
Here is the situation:
I have a gallery of pictures. When the user hovers over one of them a side div populates with more info called in by AJAX.
I just can't get it to work. I keep getting this in Dev Tools>Network>XHR :
Not Found
The requested URL /ajax/getMoreInfo was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
What am I doing wrong? and if anyone wants to drop some 'best practices', I'm all ears. Specifically.. I have seen other examples where ajax parameters are passed via the URL sent with $.post (ajax/getMoreInfo/ID) and not as a variable as I do here. Am I completely wrong? (Even though I dont think that is the cause of my 404).
Here is my JS function called on hover
function showDataWindow(){
var thisClass = $(this).attr('class');
var thisIDpos = thisClass.indexOf("id-")+3;
var thisID = thisClass.substr(thisIDpos, 3);
/// alert(thisID) <- alerts correctly
$.post('ajax/getMoreInfo',
{ ID: thisID },
function(data) {
.. do stuff with data
I have a controller named ajax.php in /controllers
<?php
class Ajax extends CI_Controller {
function __construct() {
parent::__construct();
}
public function getMoreInfo()
{
$ID = $this->input->post('ID');
$this->load->model('Artist_model','',TRUE);
$more_info = $this->Artist_model->get_more_info($ID);
echo json_encode($more_info);
}
}
And my model in /models...
<?php
class Artist_model extends CI_Model {
function __construct()
{
parent::__construct();
}
public function get_more_info($ID)
{
$query = $this->db->query('SELECT * FROM `NOIRusers` WHERE `UID` = `$ID`');
if ($query->num_rows() > 0)
{
foreach ($query->result() as $result)
{
$moreInfo['memberID'] =$result->UID;
$moreInfo['firstName'] =$result->UFname;
$moreInfo['lastName'] =$result->ULname;
$moreInfo['large_image_location'] =$result->large_image_location;
..more of these..
}
}
}
Thanks for any help!
I think its an htaccess issue. Try using the following content in your .htaccess file.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]
Have you added it to the routes.php?
http://codeigniter.com/user_guide/general/routing.html
You will need to add it in here so CI knows what to do with 'ajax'.
please try this following query if you can pass the id properly.
SELECT * FROM NOIRusers WHERE UID = '".$ID."'";
normally codeigniter use like
$this->db->select('*');
$this->db->from('NOIRusers');
$this->db->where('UID',$ID);