codeigniter redirect to another directory view - php

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');
}

Related

How To Create Custom User dashboard Template On Wordpress

Have created a custom template page on my wordpress file folder through cpanel named user-dashboard and it includes 5 files on .php so, how do I make sure that every guest accessing any page on user-dashboard folder (e.g user-dashboard/settings.php) will be redirected to example.com/login and login wordpress account.
after your page creation process complete you add the below lines in function.php file
function user_default_page() {
return '/user-dashboard';
}
add_filter('login_redirect', 'user_default_page');
One more thing is set your page can view after session set and your page path should equal of return path.

Why do actions on index.php run when Wordpress loads other pages?

I have "Front page displays" set to "Your latest posts" in the Settings > Reading section of the Wordpress dashboard and I want to use front-page.php as my landing page.
I don't really have any use for index.php outside of it being a fallback. I guess as a fallback, I'd just want to display the home page, so I just included front-page.php in index.php:
index.php
<?php include('front-page.php'); ?>
The problem is that inside of front-page.php, I have some actions running.
front-page.php
<?php do_action('myaction'); ?>
When I load the site, front-page.php, these actions all seem to be running twice. Why are actions on index.php running when front-page.php is being loaded?
This lead me to remove the include() statement inside index.php and just use an empty index.php file. That can't be right though? Why are these actions running twice when the page loads?
WordPress uses a .htaccess file to redirect all request to index.php, which then loads the wanted page.
In your case, this means going to /front-page.php first loads index.php which then loads front-page.php.
In order to redirect to front-page.php, I would suggest you to do the following steps in index.php:
Look if the current url is index.php
If we're on the index, send a redirect header Header("Location: front-page.php"); followed by exit or die.
If not, keep loading as usual
You can always place some "test" text in the file you suspect may be loading to make sure you're seeing the correct file. Just put TEST or something in the file and see if it shows on screen. Now you know which file is loading.
It will default to index.php many times if you're intended file isn't coded right or if it's named improperly. I have had this issue before!

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
?>

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

Codeigniter session does not work after including Controller in view

I was working on a site in which on right side I am displaying some fixed type of dynamic content like event calendar, login, register etc. right side bar name is right_view.php
So first I was doing like this that I was sending parameters in every function of controller's and then in my view I was accessing right side parameters by calling right view like this
<?php $this->load->view('right_view');?>
then after login I can get my username that is stored in session.
After that I thought it is not a good approach to send parameters in every functions I just make a controller named right.php and in this controller I am passing parameters to right_view.php and after that in my view I changed my code for callig righr_view like this
<?php include(base_url().'right');?>
It display right content as I do above but one changed happen that I cannot access any of session stored variable in right side bar.
Is session does not work after including controller in view?
You're basically wanting to do a template system but going about it the wrong way. And for the record no I don't think you can (or at least should) be loading controllers into views like that.
What you want is a template file something like this:
<?php
$this->load->view('templates/header', $title);
$this->load->view('templates/sidebar',$sidebar_content);
$this->load->view('pages/'.$main_content);
$this->load->view('templates/footer');
?>
Call that template.php
Then in your controller you'd do something like this:
public function welcome()
{
$data['main_content'] = 'welcome';
$data['title']='Welcome to REfficient.com!';
$data['sidebar_content'] = 'sidebar/no_sidebar';
$data['additionalHeadInfo'] ='';
$this->load->view('templates/template',$data);
}
So if you look at the template file the first line is loading the header and including the title variable to insert into the page (header, sidebar, maincontent and footer are all their own separate PHP pages) and so on.
Now what I did (since my layout was very similar to yours) is my main sidebar file has an if statement that says if logged in show x, if not show login form.
Note - the additionalHeadInfo variable is so I can have includes like jQueryUI or something on an individual page without loading it on pages that don't need it.

Categories