How to create custom 404page - php

I want to create custom 404 page for my site.I mean when i type url as
http://example.com/anydummytext
need to redirect to custom 404page
How to create custom page for this

The simple way is create a controller you can name the error controller to any what you want.
Example
<?php
class Not_found extends CI_Controller {
public function index() {
// some content
$this->load->view('not_found');
}
}
Then on another controller you can redirect it
redirect('not_found');
Example Only
<?php
class Home extends CI_Controller {
public function index() {
$result = $this->some_model->get();
if ($result) {
// content
$this->load->view('home');
} else {
redirect('not_found');
}
}
}
The other option is in config/routes.php you can use codeigniter
$route['404_override'] = 'not_found'; // Note will not work in a subfolder

You can create a new controller or use existing controller to write down a function for loading the VIEW for 404.
class Nopage extends CI_Controller
{
/* Index function
*
* return void
*/
public function index()
{
$this->load->view('nopage_404');
}
}
}
Call the controller function in config->routes as below.
$route['404_override'] = 'nopage';

Related

How to redirect from view to controller in codeigniter?

In my header view I wrote this code:
<?php
if($this->session->userdata('logged_in')) {
$query = $this->db->get_where('instructors', array('id' => $this->session->userdata('id')));
$insdatacheck = $query->row_array();
if($insdatacheck['name'] == '') {
redirect(base_url().'user/continueregistration');
} else { ?>
<script type="text/javascript">alert('test');</script>
<?php
}
}
?>
But it does not redirect to the following page. However, if I write this in the controller, it works properly. I wrote it in header view because I want to check it in every page where enters the user. How can I improve it and write in a proper way? Thanks in advance
I think instead of your header you should put your check inside your controller constructor.
class Test extends CI_Controller {
function __construct() {
parent::__construct();
// if not logged-in redirect to login page
if ($this->session->userdata('logged_in') == false) {
redirect('login'); // where you want to redirect
}
}
}
Another option is to create a base controller. Place the function in the base controller and then inherit from this.
To achieve this in CodeIgniter, create a file called MY_Controller.php in the libraries folder of your application.
class MY_Controller extends Controller
{
public function __construct()
{
parent::__construct();
}
public function is_logged_in()
{
$user = $this->session->userdata('user_data');
return isset($user);
}
}
Then make your controller inherit from this base controller.
class X extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function do_something()
{
if ($this->is_logged_in())
{
// User is logged in. Do something.
}
}
}
First create session in the controller only,
Then we access session in any page,
$this->load->library('session');
$user=$this->session->userdata('logged_in');
if (!isset($user)) {
redirect(base_url().'user/continueregistration');
}
else {
<script type="text/javascript">alert('test');</script>
}

if codeigniter enable query string in controller but could not redirect on my controller method? why

If codeigniter enable query string in controller but could not redirect on my controller method? why?
codeigniter config.php file providing this feature you have simply to activate rewrite this query
$config['enable_query_strings'] = TRUE;
At the same time our controller could not to redirect the controller methods
For example I am used this version CodeIgniter v3.0.3 (Current version).
/home/test/Pictures/SDScreenshot from 2015-11-07
<?php
defined ('BASEPATH') or exit('No direct script access allow');
class Route extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
function index()
{
$this->setting();
}
public function setting()
{
$object=&get_instance();
$this->path();
}
public function path()
{
$this->load->view('route_view');
}
function log()
{
$base=site_url().'/Route/auto_range';
redirect($base);
}
function auto_range()
{
echo 'simple auto_range is alreafy modified';
}
}

How to access a controller method from view in CodeIgniter?

I want to do something like this in CodeIgniter:
Controller:
class Home extends CI_Controller {
public function index() {
$this->load->view('home');
}
public function user($user) {
return $user;
}
}
View (home):
Hi <?php echo user('User') ?> // call a controller method
How can i do this?
One way to get it is , returning it from the routes.php ,
Like $route['user'] = "home/user"; And this will execute the user method from the home controller.

CodeIgniter reUse/extend controller

I have my controller
class Page extends CI_Controller {
public function id() {
$this->load->model('content');
$page = $this->uri->segment(3, 0);
if($page == 0)
$page = $this->content->get_default_page($page);
$data['navigation'] = $this->content->getNav();
$data['pagename'] = $this->content->get_pagename($page);
$data['content'] = $this->content->get_content($page);
$this->load->view('main', $data);
}
}
Now I'll try to explain.
Im getting navigation, and navigation text from mysql (id, navName, navText).
then im returning those elements in views/main_view.php in url like: http://abc.com/page/id/1 etc...
Now i need to create other controller like mySuperDuperModule which have some functions not just text.
The problem is that if i create new controller like Gallery(), i need to copy all the stuff from Page() controller to make website show the same.
Is there any way not to do that ?
You can create base controller under /application/core/MY_Controller.php
Here MY_ is the value specified for $config['subclass_prefix'] in /application/config/config.php
class MY_Controller extends CI_Controller
{
protected $data;
public function __construct()
{
parent::__construct();
$this->data = array();
}
public function loadPage($page)
{
$this->load->model('content');
if($page == 0)
$page = $this->content->get_default_page(); // I hope this function will give default page from database table. (A Change from your code)
$this->data['navigation'] = $this->content->getNav();
$this->data['pagename'] = $this->content->get_pagename($page);
$this->data['content'] = $this->content->get_content($page);
}
}
Your modified Page Class in /application/controllers/page.php
class Page extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function id()
{
$page = $this->uri->segment(3, 0);
parent::loadPage($page);
$this->load->view('main', $this->data);
}
}
and your new gallery controller can be in /application/controllers/gallery.php
class Gallery extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function id()
{
$page = 10; // Assuming gallery page index is 10 in database table OR you can change any logic here.
parent::loadPage($page);
$this->load->view('main', $this->data);
}
}
you can create as many controllers as you want by extending MY_Controller.
You can pass these two lines to any controller
and they will display the same template.
$data['navigation'] = $this->content->getNav();
$data['pagename'] = $this->content->get_pagename($page);
If you still dont understand try to use this library
https://github.com/philsturgeon/codeigniter-template

How to set the page title for controller actions?

I want to have different title (in head) for each Controller and action.
How to do this from the controller?
Your Controller
class SiteController {
public function actionIndex() {
$this->pageTitle = 'Home page';
//...
}
//..
}
Layout file
<title><?php echo $this->pageTitle; ?></title>
Maybe you forgot to add reference in your html?
If you want to have a different title in each action
Then simply set the value of CController.pageTitle inside your action:
class MyController extends CController {
public function actionIndex() {
$this->pageTitle = "my title";
// other code here
}
}
If you want to share a specific title between multiple actions
One way would be to simply follow the above approach, possibly by using a class constant as the page title:
class MyController extends CController {
const SHARED_TITLE = "my title";
public function actionIndex() {
$this->pageTitle = self::SHARED_TITLE;
// other code here
}
public function actionFoo() {
$this->pageTitle = self::SHARED_TITLE;
// other code here
}
}
However, this requires you to visit each action separately whenever you want to include or exclude it from the "title sharing" scheme. A solution that does not have this drawback is to use a filter. For example:
class MyController extends CController {
public function filters() {
// set the title when running methods index and foo
return array('setPageTitle + index, foo');
// alternatively: set the title when running any method except foo
return array('setPageTitle - foo');
}
public function filterSetPageTitle($filterChain) {
$filterChain->controller->pageTitle = "my title";
$filterChain->run();
}
public function actionIndex() {
// $this->pageTitle is now set automatically!
}
public function actionFoo() {
// $this->pageTitle is now set automatically!
}
}
If you want to have the same title across all actions
This is obvious, but I mention it for completeness:
class MyController extends CController {
public $pageTitle = "my title";
public function actionIndex() {
// $this->pageTitle is already set
}
public function actionFoo() {
// $this->pageTitle is already set
}
}
You can use the function init or before action or run which call before the actual action call. So In that function you can set the public pageTitle variable for the controller.
Use Like this:
public function init()
{
parent::init();
$this->pageTitle = "My Page Title";
}
Yon can give like this:-
$this->set("title", "Enrollment page");
and use this $title in ur ctp files by giving different name or title..
try this..
In the VIEW page (index.php, view.php, create.php etc)
$this->setPageTitle('custom page title');

Categories