I am building my first CodeIgniter website and I am conquering a problem that seems to be hard to solve..
When I click on a article to read it I keep getting a 404 page. ( not found )
the link that I get is as follows
index.php/articles/articlename
In my controller I have the following for the view part
public function view($slug) {
$data['article_item'] = $this->articles_model->get_article($slug);
if (empty($data['article_item']))
{
show_404();
}
$data['title'] = $data['article_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('articles/view', $data);
$this->load->view('templates/side', $data);
$this->load->view('templates/footer');
}
As you see it has to include the view.php in the articles directory
In my router I have the following
$route['articles/(:any)'] = 'articles/view/$1';
$route['articles/create'] = 'articles/create';
$route['(:any)'] = 'pages/view/$1';
$route['gallery'] = 'gallery';
$route['articles'] = 'articles';
$route['default_controller'] = 'home';
$route['404_override'] = '';
Just to be sure I have posted every relevant stuff here is the link that I create
Read more
and the model
<?php
class Articles_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_article($slug = FALSE) {
if ($slug === FALSE) {
$query = $this->db->get('articles');
return $query->result_array();
}
$query = $this->db->get_where('articles', array('slug' => $slug));
return $query->row_array();
}
}
I hope someone can tell me what I'm doing wrong here..
You are missing the function name in your link.
Your URL should be: index.php/articles/view/articlename
The first part in your URL after index.php is the name of your Class and the second part is the name of your function and then your input values
Related
I'm following the Codeigniter tutorial: http://localhost:8080/myproject/user_guide/tutorial/news_section.html
When i point my browser to http://localhost:8080/myproject/news as the tutorial indicates at the final of the section, "Point your browser to your document root, followed by index.php/news and watch your news page." appears a blank page.
I tried to point to localhost:8080/index.php/news or /myproject/news but occurs the same problem.
Also i tried to set in autoload.php the next: $autoload['libraries'] = array('database');
as is indicated in codeigniter news section tutorial not working but not solve, so i leave it like this: $autoload['libraries'] = array('');
This is the routes:
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
The News.php:
<?php
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
$this->load->helper('url_helper');
}
public function index()
{
$data['news'] = $this->news_model->get_news();
}
public function view($slug = NULL)
{
$data['news_item'] = $this->news_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
}
the view.php:
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];
and the News_model.php:
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' =>
$slug));
return $query->row_array();
}
}
I expect to watch the news page
Second EDIT - Found the Problem, and answered it as well.
First Edit - added my Post_model.php file as well for clearer explanation of my code.
I am trying to redirect a Blogs slug to a separate page where I can show the entire blogs content.
here's an example slug
http://localhost/aag/posts/test-one
Here's the Posts controller
<?php
class Posts extends CI_Controller {
public function index(){
// Shows a blog listing
}
public function view($slug = NULL){
$data['post'] = $this->post_model->get_posts($slug);
if(empty($data['post'])){
show_404();
}
$data['title'] = $data['post']['title'];
$this->load->view('templates/header');
$this->load->view('posts/view', $data);
$this->load->view('templates/footer');
}
}
The posts/view.php file
<h2><?php echo $post['title']; ?></h2>
<small class="post-date">Created on <?php echo $post['created_at']?></small><br>
<div class="post-body">
<?php echo $post['body']; ?>
</div>
the Post_model.php
class Post_model extends CI_Model
{
public function __construct()
{
$this->load->database();
}
public function get_posts($slug = FALSE)
{
if ($slug === FALSE) {
$query = $this->db->get('posts');
return $query->result_array();
}
$query = $this->db->get_where('posts', array('slug' => '$slug'));
return $query->row_array();
}
}
routes.php
$route['posts/(:any)'] = 'posts/view/$1';
$route['posts'] = 'posts/index';
$route['(:any)'] = 'pages/view/$1';
Okay, so years of writing mysqli queries made me do this mistake. In my Post_model.php I am getting the data where (DB column field) slug should match the $slug that gets passed in, but I surrounded the slugs inside single quotes that was causing the error. Here's the working code now.
class Post_model extends CI_Model
{
public function __construct()
{
$this->load->database();
}
public function get_posts($slug = NULL)
{
if ($slug === NULL) {
$query = $this->db->get('posts');
return $query->result_array();
}
$query = $this->db->get_where('posts', array('slug' => $slug));
return $query->row_array();
}
}
TLDR: Don't encapsulate your passed in arguments inside quotes.
I'm trying to load a specific page within a controller. I followed the Codeigniter tutorial and the main pages work but the individual page (loaded with view) doesn't load according to the given slug.
blog.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Blog extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/index
* - or -
* http://example.com/index.php/Index/index
* - or -
*/
function __construct()
{
parent::__construct();
$this->load->model('blog_model');
$this->load->helper('url_helper');
}
public function index()
{
$data['post'] = $this->blog_model->get_posts();
$data['title'] = 'Blog archive';
$this->load->view('header', $data);
$this->load->view('blog', $data);
$this->load->view('footer', $data);
}
public function view($slug = NULL)
{
$data['post'] = $this->blog_model->get_posts($slug);
if (empty($data['post']))
{
show_404();
}
$data['title'] = $data['post']['title'];
$this->load->view('header', $data);
$this->load->view('post', $data);
$this->load->view('footer', $data);
}
}
blog_model.php
<?php
class Blog_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_posts($slug = FALSE)
{
if ($slug === FALSE)
{
$this->db->select('*');
$this->db->from('blog_posts');
$this->db->join('category', 'category.id = blog_posts.category_id');
$this->db->join('author', 'author.id = blog_posts.author_id');
$query = $this->db->get();
return $query->result_array();
}
$this->db->select('*');
// $this->db->from('blog_posts');
$this->db->join('category', 'category.id = blog_posts.category_id');
$this->db->join('author', 'author.id = blog_posts.author_id');
// $this->db->where('slug', $slug);
$query = $this->db->get_where('blog_posts', array('slug' => $slug));
return $query->row_array();
}
}
As you can see I tried a few combinations because I'm not sure it's retrieving the table in get_posts when slug is not false.
try like this
call url_helper helper in __construct like following
$this->load->helper('url');
Now, update the routes.php like following
If your URL like
http://www.example.com/blog/view/slug
Your ruote should be like this
$route['blog/view/(:any)'] = 'blog/view/$1';
If your URL like
http://www.example.com/view/slug
Your ruote should be like this
$route['view/(:any)'] = 'blog/view/$1';
And, your get_posts model function repeating queries, use it simply like below
public function get_posts($slug = FALSE){
$this->db->select('*');
$this->db->from('blog_posts');
$this->db->join('category', 'category.id = blog_posts.category_id');
$this->db->join('author', 'author.id = blog_posts.author_id');
if($slug){
$this->db->where(compact('slug'));
}
$query = $this->db->get();
return ($query->num_rows() > 1) ? $query->result_array() : $query->row_array();
}
In the end I solved it by modifying the routes:
At first I had:
$route['default_controller'] = 'Index';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['blog'] = 'blog';
$route['blog/(:any)'] = 'blog/$1';
So I changed it to:
$route['default_controller'] = 'Index';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['blog'] = 'blog';
$route['blog/(:any)'] = 'blog/view/$1';
And changed the method name in blog.php from blog to view. "view" looks more standard than "blog" so I left it that way. In 'blog/view/$1' represents the controller, view the method and of course $1 is the first param. Actually if I try Blog/view/hello-world it works too.
I am building my comment module and trying to get the active records to display on view. Seems simple however I have a lot going on and using a separate model and controller then what the view is being generated from.
So I have a profile page ( where I'm trying to get the results to display )
Controller:
public function profile()
{
$this->load->helper('url'); //Include this line
$this->load->helper('date');
$this->load->library('session');
$this->load->library('form_validation');
$session_id = $this->session->userdata('id'); //Ensure that this session is valid
//TRYING TO MAKE A VARIABLE WITHT THE $_GET VALUE
$user_get = $this->uri->segment(3); // Modify this line
// LOAD THE CORRECT USER
$this->load->model('account_model');
$user = $this->account_model->user($user_get); //(suggestion) you want to pass the id here to filter your record
$data['user'] = $user;
$data['session_id'] = $session_id;
if($user_get != $user['id'] || !$user_get)
{
$data['main_content'] = 'account/notfound';
$this->load->view('includes/templates/profile_template', $data);
}
else
{
if($user_get == $session_id) //Modify this line also
{
$data['profile_icon'] = 'edit';
}
else
{
$data['profile_icon'] = 'profile';
}
$sharpie = $this->sharpie($user);
$data['sharpie'] = $sharpie;
$data['main_content'] = 'account/profile';
$this->load->view('includes/templates/profile_template', $data);
}
}
Now I have a new controller for my comments I'm trying to display:
public function airwave() {
$this->load->helper('date');
$this->load->library('session');
$airwave_get = $this->uri->segment(3);
$this->load->model('community_model');
$airwave = $this->coummunity_model->airwave($airwave_get);
$data['airwave'] = $airwave;
$data['main_content'] = 'account/profile';
$this->load->view('includes/templates/main_page_template', $data);
}
with this model:
public function airwave($id=null)
{
if(is_null($id)) {
$session = $this->session->userdata('is_logged_in');
$commenter_id = $this->session->userdata('id');
}else{
$commenter_id = intval($id);
}
$query = $this->db->query("SELECT * FROM airwaves_comments WHERE from_id=$commenter_id");
if($query->num_rows()==1)
{
$data = $query->result_array();
return $data[0];
//above returns the single row you need to the controller as an array.
//to the $data['user'] variable.
}
}
and trying to display it on this view ( which is generated by the profile function )
<div class="profile_airwave_comment_text">
<?php echo $airwave['comment'];?>
</div>
I just can't seem to get it to pass the array variable I've created properly to that view?
thanks in advance.
The first glaring thing I see wrong is in you model's airwave function:
if($query->num_rows()==1)
{
$data = $query->result_array();
return $data[0];
You are assuming that there will only be one result in your query, so if there are more or less than one, your $data array will bever be set.
Also, even then you are only returning the first element in the array. I am not seeing anywhere in your question that you want to return only one.
Finally, in your view, if you now return the full array, and not only one element, you will have to do a foreach statement.
EDIT: please see suggested solution.
In your model, don't worry about the check for the amount of data in the result array. This will come later. Simply do:
return $query->result_array();
Now, keep your controller as is, but adapt your view:
<div class="profile_airwave_comment_text">
<?php
if (isset($airwave) && is_array($airwave) && !empty($airwave))
{
foreach ($airwave as $wave)
{
echo $wave['comment'];
}
}
else
{
echo 'No comments found...';
}
?>
</div>
This should work, and if not, at least let you know that no comments were fond, and thus check your database.
HTH!
If your using HMVC, simply call the module from inside the profile view, ie:
echo Modules::run('comments/profileComments', $user->id);
Else:
You would need to create a new method in the loader class, to load in the controller seperate from routing.
If your really after a quick fix until you come up with a better alternative, you could(depending if both controllers live in the same directory) just include the 'comments' controller in your 'profile' controller and try something like this:
{Cons: CI Pagination wont work!}
application/controllers/profile.php
include 'Comments' . EXT;
class Profile extends CI_Controller{
public function __construct ()
{
parent::__construct () ;
}
public function index( $user_id ){
$commentsPartialView = call_user_func_array(array(new Comments(), "getProfileComments"), array($user_id));
return $this->load->view($this->template, array(
'view' => 'profiles/_index',
'comments' => $commentsPartialView
));
}
}
-
application/controllers/Comments.php
class Comments extends CI_Controller{
public function __construct ()
{
parent::__construct () ;
}
public function getProfileComments( $user_id ){
$user_comments = ''; //pull from DB
//set third param to TRUE for partial view, without main template
return $this->load->view('comments/show', array(
'user_comments' => $user_comments
), TRUE);
}
}
In my CI project, I would like to use a full dynamic page control. So, I’ve two controller methods, which load the php files. The model control’s queries are based on url segments. All page output generated automatically in the views php file depending on url and results of database, except index.php file.
Is this a right way?
Controller
public function index()
{
$data['title'] = "Index";
$data['nav'] = $this->content_model->get_index_nav(); //TODO
$this->load->view('templates/header', $data);
$this->load->view('templates/nav', $data);
$this->load->view('templates/nav_pict', $data);
$this->load->view('pages/aktualis', $data);
$this->load->view('templates/footer', $data);
}
public function view($page)
{
$page = 'content';
$this->load->helper('text');
$this->load->helper('url');
$page = lcfirst(convert_accented_characters(urldecode($page)));
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$data['nav'] = $this->content_model->get_nav();
$data['content'] = $this->content_model->get_content();
if(empty($data['content']))
{
show_404();
}
$this->load->view('templates/header', $data);
$this->load->view('templates/nav', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
Model (Each menu has a content in database. When create a new submenu you must be add content)
public function get_content()
{
$this->db->select('content.*, mainmenu.label');
$this->db->from('content');
$this->db->join('mainmenu', 'mainmenu.id = content.katId', 'left');
$this->db->where('mainmenu.label', mysql_escape_string(urldecode(end($this->uri->segments))));
$query = $this->db->get();
return $query->result_array();
}
Routing
$route['404_override'] = '';
$route['/:any/(:any)'] = 'pages/view/$1';
$route['(:any)'] = 'pages/view/$1';
$route['Index'] = 'pages/index';
$route['default_controller'] = 'pages/index';
You're using CodeIgniter just fine, there's no need for any structure modification.