Link to static page in CodeIgniter - php

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

Related

URI routing from controller to view - codeigniter

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.

Php code not rendering in Views loaded in CodeIgniter

I have a controller which has following function
public function listartistes () {
$this->set_language();
$this->data['title'] = "Create Your Own Show Today";
$this->data['meta_title'] = "Create Your Own Show Today";
$this->data['meta_desc'] = "Create Your Own Show Today";
$this->data['content'] = 'artiste/list';
$this->data['artistes'] = $this->artistes_m->getAllArtistes();
$this->load->vars($this->data);
$this->load->view('template');
}
As you can see the controller is loading the template view and inside template view we load the 'content' file like this this
<div class="container-full">
<?=$this->load->view($content);?>
</div>
So far so good . but when I try to put any PHP code in list which is inside
/artiste/list its not showing on my browser only blank screen
The partial pattern in CodeIgniter allows consecutive views to be loaded, not embedded ones. So this means that you might need a partial for header, content and footer for example instead of an outer template and inner [content] template.
So while you may have to adjust what markup and data is in which view, you will probably be best off putting
$this->load->view($content); on the line after $this->load->view('template'); in the controller. I don't think that CodeIgniter is capable of loading views from within views.

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

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.

CodeIgniter - creating forms with .html link

I want to create a simple website with 5 pages which contains 2 simple contact us forms.
The links should be www.yoursite.com/contact.html and after submitting it should go to www.yoursite.com/success.html. Please help, I have searched a lot for it but couldn't find anything
[edit]
I have a different controllers for different forms but in the links i don't want the controller name but only the view name.html
If all you want to do is change the apperance of the URLs, then you could use CodeIgniter's routing. You'll most likely also want to remove index.php from the URL, more information is available in CodeIgniter's user guide.
For example, if you have a controller named Contact with an index() function that loads the view for the contact form, and another function success() that loads the success page view:
Then this route, in application/config/routes.php would map the URL, www.yoursite.com/contact.html to your Contact controller.
$route['contact.html'] = "contact";
This route would map www.yoursite.com/success.html to the success() function within the Contact controller.
$route['success.html'] = "contact/success";
If you want your Codeigniter URLs to have ".html" appended to them for some reason, you should modify your application\config\config.php file and change
$config['url_suffix'] = '';
to
$config['url_suffix'] = '.html';
You can see here for more info.

Categories