URI routing from controller to view - codeigniter - php

I am new to codeigniter ,
I want to redirect Login page request to this route
$route['login'] = 'TravelApi/login/';
So now http://localhost.com/codeigniter/login request should route through controller/TravelApi.php 's TravelApi class 's login() function.
Controller
public function login(){
$contents['login_url'] = $this->googleplus->loginURL();
$this->load->view('frontend/login',$contents);
}
My question is :
When request routes through above controller and then goes to frontend/login.php -- login.php file gets loaded but without header and footer.
But when I remove this route from config/routes.php
$route['login'] = 'TravelApi/login/';
then request does not route through controller and directly goes to frontend/login.php . and here it loads login.php file with header and footer.
But my need is to route from controller. and load view file with header footer.
So why it does not load header footer when routes through my controller's function ?
EDIT:
I found a function in default controller welcome.php
public function pages($alias=NULL)
{
$page='frontend/'.$alias;
$this->load->view('frontend/common/head'); // For Head Scripts
$this->load->view('frontend/common/header', $this->common_menu('TopMenu')); // For Header Content
$this->load->view('frontend/common/menus', $this->common_menu('MainMenu')); // For Menus
$this->load->view($page);
$this->load->view('frontend/common/footer'); // For Footer Content
$this->load->view('frontend/common/foot'); // For Footer Scripts
}
But still not clear that why it does not load header footer when routes through my controller's function ?

You kind of answer your own question in your edit. The header and footer are located in seperate view files, and you need to load those too. So something like this should work:
public function login(){
$contents['login_url'] = $this->googleplus->loginURL();
$this->load->view('frontend/common/head'); // For Head Scripts
$this->load->view('frontend/common/header', $this->common_menu('TopMenu')); // For Header Content
$this->load->view('frontend/common/menus', $this->common_menu('MainMenu'));
$this->load->view('frontend/login',$contents);
// For Menus
$page='frontend/'.$alias;
$this->load->view($page);
$this->load->view('frontend/common/footer'); // For Footer Content
$this->load->view('frontend/common/foot'); // For Footer Scripts
}
Note: The line $this->load->view($page); looks like it's probably the main content for the other page and here the main content should be $this->load->view('frontend/login',$contents);, if that's the case remove the $page view load.

Related

codeigniter redirect to another directory view

How can I redirect to another directory view
I have two directories in views directory as
views->
public->
login.php
admin->
admin_panel.php
in the controller, I want to redirect page if successful then
admin->adminpanel.php
else
public->login
if($this->form_validation->run()){
$this->load->view('admin/admin_panel');
}
else{
$this->load->view('public/admin_login');
}
You would do something like:
if($this->form_validation->run()){
// redirect to admin panel controller
redirect('admin/admin_panel', 'refresh');
}
// load this pages views
$this->load->view('public/header_view');
$this->load->view('public/admin_login_view');
$this->load->view('public/footer_view');
The redirect stops execution of the current controller so no need for an else statement here.
You should only code your header or footer or any common block once (except your admin panel will probably have it's own different header view file). You can then chain your load view calls to create a page of any layout re using blocks used on other pages if required.
The 'refresh' in the redirect just means that the url will also change as if the user had actually clicked a link with that url on it. Without it the original form_post url will show.
Hope that helps,
Paul.
First I place header and footer inside admin_panel.php
admin->admin_panel.php
<?php include('../public/public_header.php');?>
<h5 class="lead">Admin Panel</h5>
<?php include('../public/public_footer.php');?>
now I remove the header and footer line from above file
and put this in Controller file
if($this->form_validation->run()){
$this->load->view('public/public_header');
$this->load->view('admin/admin_panel');
$this->load->view('public/public_footer');
}else{
$this->load->view('public/admin_login');
}

How to display common pages in CodeIgniter

I'm new to CodeIgniter and I'm very confuse on how to load my header.php to all the other pages I made. I want my header to be included on every page I made without manually putting them so that every time I make changes to the header I won't edit the entire page. Instead I'll just make changes in the header.php. Thank You.
create a common page call layout.php
inside that
<?php $this->load->view('includes/header'); ?> //site header
<?php $this->load->view($main_content); ?> //comes from controller.
<?php $this->load->view('includes/footer'); ?> //site footer
and in controller
function privacy_policy()
{
$data['main_content'] = 'pages/privacy_policy';
$this->load->view('layout', $data);
}
and main_content should direct to page
so its
reduce your time
reduce your code
easy to understand
etc....
You can create a folder for example -Layout under view folder and add your common pages like header,footer in it.
And in your controller on calling you can do so-
function index() {
$this->data['survey'] = $this->survey_model->get_survey();
$this->data['main_content'] = $this->controller . '/show';
$this->load->view('layouts/main_content', $this->data);
}
In this case show is my view page to be displayed,main_content is a page in my layout where header and footer is mentioned.
There are many ways but the one I prefer the most is as below :
in your Controller File
SomeController.php
function someFunction() {
$this->data['main_page'] = 'someview';
$this->load->view('layouts/main_template', $this->data);
}
In your main_template.php view
<?php
$this->load->view('layouts/header'); // your common header file
// your dynamic page file you can choose which page to load by changing the value of $main_page in controller.
$this->load->view($main_page);
$this->load->view('layouts/footer'); // your common footer file
?>

WordPress - Set 2 headers in one website

i have to header like.
header-first.php
header-second.php
i create two template like first and second. when the first template is load then i want to load header-first.php and this is working. then second like first.
i have problem with when i load second template then the header-second.php is load is working but not when i go to any page then i want to load header-second.php but not it load header.php.
when i load a second template then the header-second.php is loaded working. now i going any page and view post then the header.php file is load i want to load header-second.php file. please help how can i set globally.
i try to set globally in function.php like.
global $header;
if(is_page_template('page-second.php')){
header = 'second';
}else if(is_page_template('page-first.php')){
header = 'first';
}
it working it return only template page is load.
when i try to load any other like about page it return '' value. it not return any value.
i want to second whenever the first template is not visit.
thank you.
Each template file such as page.php, single.php, archive.php, etc, will have get_header() at the top of it.
This function loads the header (header.php).
If you pass in a string as the first argument it will try to load header-{argument}.php and if not found it will load header.php.
Anywhere you want to load header-second.php you need to change get_header(); to get_header( 'second' );
There is an action that fires when this function runs but there's no filter so you can't override it globally like you're attempting to do. Instead you need to update the template files individually with the function I showed you above.
To summarise change:
<?php get_header(); ?>
To:
<?php get_header( 'second' ); ?>
Further reading: http://codex.wordpress.org/Function_Reference/get_header

Implementing Pjax on a common header footer coding structure

I am developing a project using codeigniter that has common header and footer. By using pjax I am able to dynamically change the content alone without disturbing the header and the footer. Also the url changes with respect to the controller. Below is my concern over the url and SEO analogy.
My default home page controller loads the header, index page and the footer as shown below.
public function index(){
$this->load->model('dbmodel');
$data['about'] = $this->dbmodel->about();
$this->load->view('templates/header',$data);
$this->load->view('includes/index',$data);
$this->load->view('templates/footer');
}
Suppose I click on a menu item, it loads the corresonding controller path in the url (say - http://domain.com/main/bandDirectory) and the pjax content div alone is replaced/updated with the content while header and footer remaining the same.
public function bandDirectory(){
$this->load->model('dbmodel');
$data['content'] = $this->dbmodel->band();
$this->load->view('includes/bandDirectory',$data);
}
This works fine when the menu items are navigated from the home page as it loads the header and footer initially. But what if we directly hit the url (say http://domain.com/main/bandDirectory). This controller does not contain header and footer and it loads only the content which breaks the page apart! This would become a serious issue if search engines indexes these urls. How to overcome this issue?
P.S : Since I am implementing a player in the header, I do not want to include header and footer in all the controllers as this would stop the player from playing when header refreshes.
What I did in our project was that to look for pjax header in the request, if the pjax header is present then load the content template only else load the full template, this is my corresponding code in perl, hope it helps
sub tour {
my $self = shift;
return $self->render(
template => 'static/tour',
layout => $self->req->headers->header('X-PJAX') ? 'content_header' : 'full_width',
);}
you can implement the same in php

Link to static page in CodeIgniter

I'm new to PHP and CodeIgniter, and I'm having trouble setting up the routing of static pages in my CodeIgniter app.
I have the About Us | Contact Us | Privacy Policy and many other pages in my footer, which when the user clicks, should go to a url like
mysite.com\about
mysite.com\contact
mysite.com\privacy
You get the gist..
Here's what I've done in the footer div in the views folder
About |
Contact Us |
Privacy |
Terms of Use
Here's the function in a controller Home
public function staticpages($page)
{
if ( ! file_exists(APPPATH.'application/views/'.$page.'.html'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view($page, $data);
}
The static pages are just pure HTML content, with <p> fields and no dynamic or javascript stuff. So I have them saved as .html; contact.html, about.html and so on.
I just want to load those pages' content in a contentbody div I have created, between the header and the footer, and that's the only thing that changes in the whole site for whatever the user clicks.
When I goto http://localhost/myapp/home/staticpages/about
I get the
404 Not Found error
The requested URL /dissekted/home/staticpages/about was not found on this server.
And it's the one that the browser throws.
config your base url
$config['base_url'] =
load the helper
first method
$this->load->helper('url');
or second method
autoload it by changing application/config/autoload.php
use with
$this->config->base_url();
first check with echo you are getting base url or not
<?php echo base_url(); ?>
and use with your static link
<a href="<?php echo base_url(); ?>/static_file " />static link</a>
ok
make this inside in controllers function like
public function static ()
{
$this->load->view('static.html');
}
Check your base URL in the config file and make sure you have the final / on the path:
$config['base_url'] = 'http://localhost/myapp/'
Also, I use the anchor() function from the URL Helper to take create my links.
That takes care of forming the links correctly and takes into account the base URL.
create privacy.php in view folder and use below code to display it.
$this->load->view('page_name'); no need to use extension like static.php.
function privacy() {`enter code here`
$data['meta_title'] = 'Privacy Policy';
$data['title'] = 'Privacy Policy';
$data['main'] = 'root/privacy';
$this->load->vars($data);
$this->load->view($this->template or page_name);
}

Categories