Codeigniter form_open 404 - php

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.

Related

page redirect issue codeigniter

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');
}
}
}

Error 404 when routing

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

Unable to redirect to a new page - CodeIgniter

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

Failed to Run any "Model/Controller in Browser using YII and gii ?

I am newbie in Php using YII and Gii when iam trying to open any model and controller file in browser i am getting error the console of error and code is:
CODE:
<?php
class UsersController extends Controller {
public function actionIndex() {
$this->render('index');
}
public function actionLogIn() {
if (isset($_POST['user_name']) && !empty($_POST['user_name']) && isset($_POST['password']) && !empty($_POST['password'])) {
$username = strtolower($_POST['user_name']);
$pass = MD5($_POST['password']);
$record = Users::model()->find('user_name=:username and password=:pass and status=:status', array(':username' => $username, ':pass' => $pass, ':status' => 'ACTIVE'));
if($record){
$response['status'] = true;
$response['message'] = "Successfully Logged In.";
$response['user'] = $record;
} else {
$response['status'] = false;
$response['message'] = "Username Or Password did not Match.";
}
echo CJSON::encode($response);
}
}
}
Here is the error which appears in browser
Can someone please point me what does this error mean ?
try this url
localhost/projectName/moduleName/controllerName/actionName
Watching your question I think your are trying to use this Url
localhost/projectName/services/users
First of all, welcome to Yii!
Can someone please point me what does this error mean ?
The php code in your UsersController.php is being parsed and it's seen that your class UsersController extends the class Controller, which it can't find because it hasn't been imported (along with the rest of the framework).
i am trying to open any model and controller file in browser
If you want to test your actionIndex() method in your UsersController class (in your UsersNew module), you need to do it using this url:
localhost/examapp/usersNew/users/index
Make sure you have the urlmanager enabled (uncommented) in your config main.php file: /protected/config/main.php, like this:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
Hope that helps.
Edit:
When using the urlmanager, you should add a .htaccess file to the root containing the following:
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
Because you're using wamp, you should check that it has the rewrite_module enabled:

How to run function of another controller with base_url specified in config.php

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)

Categories