dynamic Page control in codeigniter - php

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.

Related

Can't load a specific page on codeigniter "slug"

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.

Codeigniter Cache a single

I have the following functions :
class Pharmacy extends MY_Controller {
function __construct() {
parent::__construct();
}
function index() {
$data['pack'] = $this->package();
$data['name'] = $this->getName();
$data['title'] = $this->title();
$data['name'] = $this->getName();
$data['tests'] = $this->get_tests();
$data['patient_bio'] = $this->patient_bio();
$data['procedure_name'] = $this->getProcedure();
//Pass the contents of pharmacy view to the template view.
$data1['contents'] = 'pharmacy';
$finaldata = array_merge($data, $data1);
$this->base_params($finaldata);
}
function base_params($data) {
//Load the base template for the function.
$this->output->cache(3000);
$data['title'] = 'Pharmacy';
$this->load->view('template', $data);
}
}
I want to cache the foloowing view template but when I pass the $this->output->cache(3000); it caches the whole webpage instead of caching only the template view. How can I cache only the template view ?

Codeigniter error 404 on news item tutorial

I saw plenty of questions and tried all those fixes similar to this issue but could not get this resolved. Please help.
I am following codeigniter news item tutorial. I displayed news from database. When I click on a link to view particular news it throws 404.
Here is the code:
Controller:
<?php
class News extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('news_model');
$this->load->helper('url');
}
public function index() {
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
public function view($slug) {
$data['news_item'] = $this->news_model->get_news($slug);
// var_dump($data);exit;
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');
}
}
Model:
<?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();
}
}
View:
news.php
<?php
class News extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('news_model');
$this->load->helper('url');
}
public function index() {
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
public function view($slug) {
$data['news_item'] = $this->news_model->get_news($slug);
// var_dump($data);exit;
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');
}
}
view.php
<?php
echo '<h2>' . $news_item['title'] . '</h2>';
echo $news_item['text'];
routes:
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
Base url is blank in config.
CI version is 2.1.4 (latest)
This URL shows all the news items correctly - http://localhost/educon/index.php/news/
This URL throws 404 - http://localhost/educon/index.php/news/view/n1
The problem is in your routes. Your routes file have the line as
$route['news/(:any)'] = 'news/view/$1';
But your url is
http://localhost/educon/index.php/news/view/n1
This will redirect to
http://localhost/educon/index.php/news/view/view/n1
In your view($slug) method you have a parameter. You dump this $slug in first line of view() and check it.
I had a similar issue. For me, the routes.php file was fine (by "fine" I mean it matched what the tutorial said to do (also version 2.1.4)), but my views/news/index.php file generated the news article link like so:
<p>View article</p>
After noting the issue with the generated path, I dropped news from the path and this worked:
<p>View article</p>
So links went from: /news/news/news-item-2 to /news/news-item-2
And given the tutorial's routing:
$route['news/(:any)'] = 'news/view/$1';
That then made sense.
I encountered this just now, and I also just copied everything in the tutorial, the thing is, it is in the arrangement of the routing, at first, I just added the new codes in routes.php like this....
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
$route['news'] = 'news';
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
then change it to
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
And now it works like a charm...
Using slug without spaces worked for me.
The 'News Section' part of the tutorial tells you to create some seed records. In my case, my slug contained spaces and I got the 404 error. Removing the spaces fixed this. (CodeIgniter v.3.1.4)
try to check your link that goes to news listing it should be something like this
<li>News</li>
you need to change it to read as follows, because i tried it out and its the fix for the problem
<li>News</li>
It will be great if someone with more experience can guide on how to correct this in the route.php
below is my route.php
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news/index';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
note it will work even if you make the second line as:
$route['news'] = 'news';
and .htaccess is as:
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|js|css|uploads|favicon.png)
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]
best

getting a 404 error while clicking on a article

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

How to call multiple models on one page (Codeigniter equiv of Cake Elements)

I'm just getting familiar with Codeigniter (2.1.0) and building a simple site, that has a table for products and a table for locations. I have followed the CI documentation and successfully built a page to display data from each table. But my goal is to show both sets of data on the home page. CakePHP handles this nicely with "elements", where the data from an unrelated controller can be shown on a page handled by a different controller. Seems simple and I'm having a tough time finding a starting point.
Here is the index function from the products controller:
public function index()
{
$data['product'] = $this->product_model->get_product();
$data['title'] = 'title of page';
$this->load->view('templates/header', $data);
$this->load->view('product/index', $data);
$this->load->view('templates/footer');
}
I gave a shot to include the locations model by creating a function that called that model also in the products controller, and then included the function in the above with $this->functionName();
That did not work. Any advice on where to begin?
here is procedure to include the model
include model
call a specific method
yes it is possible you can include model as you can
Public function index()
{
$this->load->model('mymodel');
$this->load->model('mymodel2');
$this->load->model('mymodel3');
$data['product'] = $this->product_model->get_product();
$data['result'] = $this->mymodel->abc();
$data['result2'] = $this->mymodel2->xyz();
$data['title'] = 'title of page';
$this->load->view('templates/header', $data);
$this->load->view('product/index', $data);
$this->load->view('templates/footer');
}
simply load the other model in this method
public function index(){
$this->load->model('products');
$this->load->model('locations');
$products = $this->products->index();
$locations= $this->locations->index();
$data['products'] = products ;
$data['locations'] = locations;
$this->load->view('templates/header', $data);
$this->load->view('product/index', $data);
$this->load->view('templates/footer');
}

Categories