Hello, this is php in codeigniter. I have posted the same topic in codeigniter discussion forum, but I get a really long time to get a reply and some times do not get a reply at all. So may be if there are some codeigniter experts in this forum might be great. :).

controllers/pages.php

<?php

class Pages extends CI_Controller {

public function create() 
 {
 $this->load->helper('form');
 $this->load->library('form_validation');

 $data['title'] = 'Create a news item';

 $this->form_validation->set_rules('title', 'Title', 'required');
 $this->form_validation->set_rules('text', 'text', 'required');

 if ($this->form_validation->run() === FALSE)
 {
  $this->load->view('templates/header', $data);
  $this->load->view('news/create');
  $this->load->view('templates/footer');

 }
 else
 {
  $this->news_model->set_news();
  $this->load->view('news/success');
 }
 }
} 

views/news/create.php

<h2>Create a news item</h2>

<?php echo validation_errors(); ?>

<?php echo form_open('news/create') ?>

 <label for="title">Title</label>
 <input type="input" name="title" /><br />

 <label for="text">Text</label>
 <textarea name="text"></textarea><br />

 <input type="submit" name="submit" value="Create news item" />

</form> 

I point the url to: http://localhost/News/index.php/pages/create

This error appears:

404 Page Not Found

The page you requested was not found.

Why is it?

can you show us your config/routes.php? e.g

$route['pages/create'] =  'pages/create';

Yes, News is a rename from Codeigniter folder.

http://localhost/News/index.php/pages/create

config/routes.php

    $route['news/create'] = 'news/create';
    $route['news/(:any)'] = 'news/view/$1';
    $route['news'] = 'news';
    $route['(:any)'] = 'pages/view/$1';
    $route['default_controller'] = "pages/view";

Yes, News is a rename from Codeigniter folder.

It is not clear to me. So you have a setup similar to this:

/
/News/
/News/index.php
/News/system/
/News/application/

Correct? Be aware that News and news are different paths for the system. So if the name of the directory is all lower case your link will be:

http://localhost/news/index.php/pages/create

If this is the route you expect to catch, I think it should be changed

$route['(:any)'] = 'pages/view/$1';

to

$route['pages/(:any)'] = 'pages/$1';

After having my eyes refreshed from looking at too many codes, I just noticed that your controller is missing lots of stuffs. For example, the contructor is missing. There should be a constructor here if you want to initialize the news_model and use its method called set_news().

Something like this

public function __construct()
{
    parent::__construct();
    $this->load->model('news_model');
}

because your original codes has this

 $this->news_model->set_news();

and even this, you have pointing into the view/news directory

 $this->load->view('news/create');

so, if your view files for the pages controller is located in the views/pages, then you will have to adjust everything including the routes.php .

It is also recommended to have a model class for your application. It can be called as pages_model.php or news_model the choice is yours. However, I highly recommend for you use the pages_model.php, because this will eliminate some the potential naming collision in the future.

Hello, thanks. Now, the program works. I begin to understand the concept of routing.

What does this means:

    $this->load->view('templates/header', $data);
    $this->load->view('news/create');
    $this->load->view('templates/footer');

Can you load 3 php pages at the same time ? Or does it mean that it loads them in vertical order ?

Yes. You are loading 3 pages in vertical order.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.