page not found error in codeigniter - php

I have anchor in header.php as below:
Backend
Also have the controller named backend.php in Controllers folder.
I also routed this code in routes.php file like
$route('default_controller')='frontend';
$route('backend')='backend';
Backend controller page is:
<?php
class Backend extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function index(){
$data['title'] = 'Backend Page';
$this->load->view('templates/header', $data);
$this->load->view('backend/reg', $data);
$this->load->view('templates/footer');
}
}
Although getting error 404 Page not found.

Make sure all controller and models etc Ucfirst example Frontend.php instead of frontend.php
Codeigniter Docs http://www.codeigniter.com/docs
Auto load url helper in application.config/autoload.php
Change route from
$route('default_controller') ='frontend';
to
$route['default_controller'] = "frontend";
$route['backend'] = "backend";
I you have not set up you config to remove index.php with htaccess then use index.php in link.
Backend
Example controllers/Backend.php
<?php
class Backend extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function index(){
$data['title'] = 'Backend Page';
$this->load->view('templates/header', $data);
$this->load->view('backend/reg', $data);
$this->load->view('templates/footer');
}
}
Example controllers/Frontend.php
<?php
class Frontend extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function index(){
$data['title'] = 'Frontend Page';
$this->load->view('templates/header', $data);
$this->load->view('frontend/reg', $data);
$this->load->view('templates/footer');
}
}
Link here for htaccess examples for removing index.php on windows os https://github.com/riwakawebsitedesigns/htaccess_for_codeigniter

Try this one :
Backend // include also the folder where your backend file is stored
I hope this helps.

Related

How to set up CodeIgniter controllers for similar forms in different directories

I'm using CodeIgniter to access a variety of form types
I have a directory such as this:
-views
--resources
---app1
----form.php
---app2
----form.php
---app3
----form.php
---app4
----form.php
My class is currently very basic, but this
class Resources extends CI_Controller {
public function app1($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('resources/app1/form.php', $data);
$this->load->view('templates/footer', $data);
}
public function app2($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('resources/app2/form.php', $data);
$this->load->view('templates/footer', $data);
}
}
This seems very verbose and unnecessary to have a method for every form. However I can't find how I can change the directory without creating a new method. I would ideally like a method where I can pass in a new directory as an arg like $page can be. Eg:
class Resources extends CI_Controller {
public function view($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('resources/'. $folder. '/form.php', $data);
$this->load->view('templates/footer', $data);
}
}
However, it seems CodeIgniter doesn't allow this. Can anyone suggest a way in which this can work?
Actually you can.
Create a base_controller inside your core folder and call it MY_Controller.php and make it extends CI_Controller and create a method inside MY_Controller and name it render, render_view, view whatever you want and inside that function load you layout partials and template and just pass the view to it: application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
protected $data = array();
public function render_view($view = '')
{
$this->load->view('templates/header', $this->data);
$this->load->view('view_path/'. $view, $this->data);
$this->load->view('templates/footer', $this->data);
}
}
and for every controller in your application make it extend MY_Controller and whenever you wanna render a view use render_view($view) and you got you header and footer preloaded, and that's the simplest way of making it DRY.
Finally in your controller it should be like this:
class Resources extends CI_Controller {
public function app1($page = '')
{
// $data array in my_controller, it will automatically be passed inside render_view
$this->data['title'] = ucfirst($folder); // Capitalize the first letter
$this->render_view('app1/form');
}
public function app2($page = '')
{
$this->data['title'] = ucfirst($folder); // Capitalize the first letter
$this->render_view('app2/form');
}
}
Calling your function "view" is almost certainly a bad idea... it's used by CI for the $this->load->view() for starters.
public function app_form($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('resources/'. $page. '/form.php', $data);
$this->load->view('templates/footer', $data);
}
That should work but how are you going to call the functions? Via the routes.php file?

CodeIgniter Error opening Home Page

I create a Home Page and when I want to access it's written Page Not Founded
I create in my rount.php access but again there is same error
$route['pages/index']= 'pages/home';
Do I need to change my controller or model or just route.php file
Pages controller
<?php
class Pages extends CI_Controller{
public function index(){
$this->load->view('pages/home');
}
public function view($page='home'){
if(!file_exists(APPPATH. 'views/pages/'.$page.'.php')){
show_404();
}
$data['title']= ucfirst($page);
$this->load->view('templates/header');
$this->load->view('pages/'.$page,$data);
$this->load->view('templates/footer');
}
}
try like this . and make sure you are linking right base_url for home index function like this. follow this if its help .
echo base_url("pages/home");
$route['pages/home']= 'pages/index';

Doubts in integration with PHPExcel and CodeIgniter

I'm with a doubt at this post:
http://www.ahowto.net/php/easily-integrateload-phpexcel-into-codeigniter-framework/
I've done up to libraries part (Excel.php).
But in the tutorial, where it starts Example Usage, where exactly I need to put all that code? In a new controller? Here in my project I tried to create a new Controller called Report. In report I've this code:
public function readReport() {
$this->load->library('excel');
$this->excel=PHPExcel_IOFactory::load(APPPATH."/third_party/teste.xlsx");
$this->excel->setActiveSheetIndex(0);
//get some value from a cell
$number_value= $this->excel->getActiveSheet()->getCell('C1')->getValue();
$data['header'] = $number_value;
$this->load->view('pages/home', $data);
}
But I have also a Pages controller to control the pages of Views, and when I try to output something of PHP Excel is not possible. In my Pages.php I've wrote: $data['header'] = $number_value; and in view . But the variable "number_value" is not in Pages.php because it's only at Report.php. How can I do to output the excel data at my home.php (view) correctly?
Here is my pages.php controller
class Pages extends CI_Controller {
public function view ($page = 'home') {
if (!file_exists(APPPATH.'views/pages/'.$page.'.php')) {
show_404();
}
$data['title'] = str_replace("_", " ", $page);
$data['header'] = $number_value;
$this->load->helper('url');
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
I'm not very familiar with PHPExcel but here are some thoughts.
First, to use a library in a Controller it must be loaded in that controller. You cannot access one controller from another. You could duplicate the code from Report in Pages but that seems a waste. You need reuseable code.
One "reuseable" approach is to create a model that uses the excel library. This will make it easy to reuse PHPExcel code just by loading the model in any controller.
A model version including a readReport() function might look like this.
class excel_model extends CI_Model
{
protected $excel;
function __construct()
{
parent::__construct();
$this->load->library('excel');
}
function readReport()
{
$this->excel = PHPExcel_IOFactory::load(APPPATH."/third_party/teste.xlsx");
$this->excel->setActiveSheetIndex(0);
//get some value from a cell
return $this->excel->getActiveSheet()->getCell('C1')->getValue();
}
}
Pages controller should be modified as follows.
class Pages extends CI_Controller
{
public function view($page = 'home')
{
//The following check isn't needed, codeigniter will do this automatically
//if (!file_exists(APPPATH.'views/pages/'.$page.'.php')) {
//show_404();
//}
$this->load->model('excel_model');
$this->load->helper('url');
$data['title'] = str_replace("_", " ", $page);
$data['header'] = $this->excel_model->readReport();
$this->load->view('templates/header', $data);
//You don't have to keep sending $data to the views because
//any variables loaded by the first call to load->view()
//will be visible the all other views loaded in this function.
$this->load->view('pages/'.$page);
$this->load->view('templates/footer');
}
}

controller for view not working codeigniter

I have two views ie, views/pages/home.php and views/pages/search_result.php. I have a controller to load this views ie, controllers/Pages.php. And also i have one more folder inside view ie, views/templates/header.php and views/templates/footer.php
When i am pointing the browser to http://localhost/codeigniter/home everything working perfect.
But the problem is when i am pointing the browser to http://localhost/codeigniter/search_result, the view footer.php is also showing. Actually am not given anything inside search_result.php
My controller code is,
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pages extends CI_Controller {
public function home($page = 'home')
{
//code to show home.php (http://localhost/codeigniter/home)
if (!file_exists(APPPATH.'/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
else
{
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
public function search_result($page = 'search_result') {
//code to show search_result.php (http://localhost/codeigniter/search_result)
}
}
Inside search_result function i didn't given any code and while am pointing to http://localhost/codeigniter/search_result the footer from function home is showing ie, $this->load->view('templates/footer', $data);
What am doing wrong. Is there any solution to solve this issue. I am beginner in codeigniter.
Just simply try this
public function home()
{
//code to show home.php (http://localhost/codeigniter/home)
if (!file_exists(APPPATH.'/views/pages/home.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
else
{
$this->load->view('templates/header', $data);
$this->load->view('pages/Home', $data); # changed
$this->load->view('templates/footer', $data);
}
}

Error when trying to load view in my_controller

Consider my code:
<?php
class MY_Controller extends Controller {
public function __construct()
{
parent::Controller();
}
function _displayPage($page, $data = array()) {
$this->load->view('structure/header', $data);
$this->load->view($page, $data);
$this->load->view('structure/footer', $data);
}
}
?>
page.php
<?php
class Page extends MY_Controller {
function __construct() {
parent::__construct();
}
function index() {
$data['content'] = array('title'=>'hello world');
$this->_displayPage('home', $data);
}
}
?>
Upon loading my page on my browser, I get this error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Page::$view
Filename: libraries/MY_Controller.php
Line Number: 11
Does anyone know what I'm doing wrong?
Thanks
In your library My_Controller you should be using the parent keyword instead of $this.
your code should look like so:
class MY_Controller extends Controller
{
public function __construct()
{
parent::Controller();
}
function _displayPage($page, $data = array())
{
parent::load->view('structure/header', $data);
parent::load->view($page, $data);
parent::load->view('structure/footer', $data);
}
}
If I understand what you're trying to accomplish correctly, you're wanting to setup a template that includes your header view and footer view, but without calling header and footer views for each controller you use throughout your application. Here's what I've done to accomplish this.
First, create a new folder under your views, for this example we'll call it 'includes'. Inside the newly created includes folder, create three files, header.php, footer.php and template.php. Setup your header and footer appropriately and then edit your template.php to look as follows:
<?php echo $this->load->view('includes/univ_header'); ?>
<?php echo $this->load->view($main_content); ?>
<?php echo $this->load->view('includes/univ_footer'); ?>
Now, from your controller you can define what view you would like to set as your 'main_content'. For example, if you have home.php in your views folder and you want to wrap it with your header and footer you would do so in your controller as follows:
function index() {
$data['content'] = array('title'=>'hello world');
$data['main_content'] = 'home';
$this->load->view('includes/template', $data);
}
Hope this helps!

Categories