CodeIgniter method Cannot get File Path Correctly - php

I parsed a html into Header, Sidebar, Content, Footer etc.. And main view put together.. It is working when I calling view from mycontroller/index but it isnt working when I calling view from mycontroller/method.. Pseude Code below;
Controller;
class welcome extends CI_Controller{
public function index(){
$data['content'] = "**main**_page";
$this->load->view('welcome_message',$data);
}
public function deneme(){
$data['content'] = "**another**_page";
$this->load->view('welcome_message',$data);
}
}
View;
<html>
<head>
<?php $this -> load -> view("includes/head_view");?>
</head>
<body>
<?php $this -> load -> view("includes/navbar_view");?>
<?php $this -> load -> view($content);?>
<?php $this -> load -> view("includes/footer_view"); ?>
</body>
</html>
Example Sub View
<img src="<?php base_url(); ?>assets/img/image1.jpg">
When I open below address, everything working fine, images loading
http://localhost/myproject
But When open below, images is not loading
http://localhost/myproject/welcome/deneme
I check the images url from Google Chrome> Page Source Code..
The link writing like that;
localhost/myproject/welcome/assets/img/image1.jpg
instead of
localhost/myproject/assets/img/image1.jpg
It's adding controller name to all links how can resolve it
Thank you

Where you define your base_url it says "URL to your CodeIgniter root. Typically this will be your base URL, WITH a trailing slash":
$config['base_url'] = 'http://localhost/myproject/';

Related

How to dynamically load css file for each action in a controller?

I have a controller named 'Student' with two actions called 'index' and 'add'.
I want to load different css files for each of the action. So far i have tried is, i have imported Html Helper, created object of it and called its css method. When i run it, it is not throwing any error, nor showing expected result. Means, it is not loading css file dynamically in my view.. How can i dynamically load css files in different views from Controller?
Code:-
<?php
App::import('Helper','Html');
class StudentController extends AppController
{
public function index()
{
// $current_controller = $this->params['controller'];
// echo $current_controller;
//$view=new View(new Controller($current_controller));
//$Html=new HtmlHelper($view);
$Html=new HtmlHelper(new View(null));
//$html=new HtmlHelper(new View());
$Html->css('cake.generics');
//echo ;
//$this->Html->css("cake.generics");
}
public function add()
{
// $current_controller = $this->params['controller'];
// echo $current_controller;
$html=new HtmlHelper(new View(null));
$html->css("mystyle.css");
}
}
You can do it in view file e.g
//in your View/Students/add.ctp
$this->Html->css('yourstyle', array('block' => 'yourStyle'));
//in your layout file
echo $this->fetch('yourStyle');
the same with js files
// in your view
$this->Html->script('yourjs', array('block' => 'yourJs'));
//in your layout file
echo $this->fetch('yourJs');
you can also create a global layout file in View > Element like default_assets.ctp
after that add this file in your default layout file like,default_layout.ctp in View > Layout folder
and after access this in your controller like
public function index(){
$this->layout = "default_layout";
}
I got it working in this way,
I have added "mycss" variable in controller, index action:-
$this->set('mycss','custom');
And accessed this mycss variable from layout file:-
if(isset($mycss)){
$this->Html->css("$mycss");
}
And it worked.

OpenCart 2.1 - Get customer First name in header.tpl

Adds: $data['customer_firstname'] = $this->customer->getFirstName(); in header.php
You may be something:
<?php
class ControllerCommonHeader extends Controller {
public function index() {
$data['customer_firstname'] = $this->customer->getFirstName();
Displays the name eg in header.tpl: <?php echo $customer_firstname ?>
Login to your store manager: go to extensions, modifications and upgrade (button top right).
Ready!
There are two header.tpl files inside OpenCart project. I think you are editing header.tpl inside public_html/catalog/controller/common/header.tpl ,so there is nothing will solve your problem editing this file.
go to the other location where second header.tpl file located. you can find it inside public_html/system/storage/modification/catalog/controller/common/header.tpl . in there you can get values by adding these,
$data['customer_firstname'] = $this->customer->getFirstName();
$data['customer_lastname'] = $this->customer->getLastName();
this will solve your problem.
try with this:
get customer information:
<?php
class ControllerCommonHeader extends Controller {
public function index() {
$customer_info = $this->model_account_customer->getCustomer($this->customer->getId());
$data['customer_firstname'] = $customer_info['firstname'];
The referenced post uses
$data['customer_firstname']
You should put your variable in the $data-Array to pass it to the template...
Adds: $data['customer_firstname'] = $this->customer->getFirstName(); in header.php
You may be something:
<?php
class ControllerCommonHeader extends Controller {
public function index() {
$data['customer_firstname'] = $this->customer->getFirstName();
Displays the name eg in header.tpl: <?php echo $customer_firstname ?>
Login to your store manager: go to extensions, modifications and upgrade (button top right).
Ready!

CodeIgniter website skeleton

I was coding PHP using controll based on adress and GET method with index.php?site=filename. Then i was including existing filename.php in content div. That was simply but effective. Also i has got notification div where i included file with data from database
I'm trying to get similar result in CodeIgniter
html
head
/head
body
div notification bar (always on top like Material)
div menu (slide from left also like Material)
div content (depended on controller and loaded view)
div footer (only /body /html)
/body
/html
by
public function __construct()
{
parent::__construct();
$this -> load -> view('notification ');
$this -> load -> view('menu ');
}
I want do send data from model to notification view but i can't do this in constructor. Also i dont want to load this same view in each controller's method. How should i do this? I dont expct ready solution, but some ideas, maybe pseudocode?
Or maybe this is only one solution like this? Loading all needed views in every methods? Really?
class news extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this -> load -> view('header');
}
public function index()
{
[...]//data from model
$this -> load -> view('notification',$Data);
$this -> load -> view('menu',$Permission);
$this -> load -> view('news',$Content);
}
[...]
I'm trying this (sorry for polish words):
controller
class uzytkownik extends CI_Controller
{
public function index()
{
$this -> load -> model('Uzytkownik_model');
$DaneLogo['Tytul'] = 'Dzie z życia';
$Dane = array(
'Naglowek' => $this -> load -> view('naglowek', NULL, TRUE),
'Logo' => $this -> load -> view('logo', $DaneLogo, TRUE),
'Lista' => $this -> Uzytkownik_model -> PobierzUzytkownikow(),
);
$this -> load -> view('uzytkownicy', $Dane);
}
view
echo $Naglowek;
echo $Tytul;
echo $Logo;
foreach ($Lista->result() as $Uzytkownik)
{
echo $Uzytkownik -> id.' ';
echo $Uzytkownik -> imie.' ';
echo $Uzytkownik -> nazwisko.'<br>';
}
working fine but...
<head>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8"/>
</head>
<body>
<div id='logo' style='background-color: blue; wight: 100%; height: 40px;'>
<center> <!-- i want $Tytul HERE --></center>
</div>
Dzie z zycia<!-- NOT HERE -->1 Jan Kowalski<br>2 Adam Nowak<br>3 Alicja Marczak<br></div>
</body>

Autoload a view on every page with codeigniter

So I have my views split up basically between three (3) files:
-- Header file
$this->load->view('templates/header', $data);
-- Main Body file
$this->load->view('login_view', $data);
-- Footer file
$this->load->view('templates/footer', $data);
Now I just recently started building, but I've noticed it's really annoying to retype the header and footer on every controller to tell it to load. Is there a way to automatically load the header and footer view on every request?
I found an article long time ago, but i can't seem to find it now, basically the author, (which i forgot) override the showing of output. this method of output will access your views regarding the given controller/method and will try to search in your views directory automatically.
Use at your own risk
Application/core/MY_COntroller.php
----------------------------------------------------------------------------
class MY_Controller Extends CI_Controller
{
protected $layout_view = 'layouts/application'; // default
protected $content_view =''; //data
protected $view_data = array(); //data to be passed
public function __construct()
{
parent::__construct();
}
public function _output($output)
{
if($this->content_view !== FALSE && empty($this->content_view)) $this->content_view = $this->router->class . '/' . $this->router->method;
$yield = file_exists(APPPATH . 'views/' . $this->content_view . EXT) ? $this->load->view($this->content_view, $this->view_data, TRUE) : FALSE ;
if($this->layout_view)
{
$html = $this->load->view($this->layout_view, array('yield' => $yield), TRUE);
echo $html;
}
}
}
Application/views/layouts/layout.php
----------------------------------------------------------------------------
<html>
<head>
<title>master layout</title>
</head>
<body>
<!-- this variable yeild is important-->
<div><?=$yield;?></div>
</body>
</html>
This is what i use to create my template. Basically you need a directory structure as follows.
+Views
|+layouts
||-layout.php
the layout.php will serve as your master template
How to use?
extend the controller
class User Extends MY_Controller
{
public function create_user()
{
//code here
}
public function delete_user()
{
//use a different master template
$this->layout_view = 'second_master_layout';
}
public function show_user()
{
//pass the data to the view page
$this->view_data['users'] = $users_from_db;
}
}
Just create directory in your views and name it with the controller name i.e user then inside it add a file you named your method i.e create_user
So now your Directory structure would be
+Views
| +layouts
| |-layout.php
| |-second_master_layout.php
| +user
| |-create_user.php
Just Edit the code to give you a dynamic header or footer
Here is the simple example which i always do with my CI project.
Pass the body part as a $main variable on controller's function
function test(){
$data['main']='pages/about_us'; // this is the view file which you want to load
$data['something']='some data';// your other data which you may need on view
$this->load->view('index',$data);
}
now on the view load the $main variable
<html lang="en">
<head>
</head>
<body>
<div id="container">
<?php $this->load->view('includes/header');?>
<div id="body">
<?$this->load->view($main);?>
</div>
<?php $this->load->view('includes/footer');?>
</div>
</body>
</html>
In this way you can always use index.php for your all the functions just value of $main will be different.
Happy codeing
Using MY_Controller:
class MY_Controller extends CI_Controller {
public $template_dir;
public $header;
public $footer;
public function __construct() {
parent::__construct();
$template_dir = 'templates'; // your template directory
$header = 'header';
$footer = 'footer';
$this->template_dir = $template_dir;
$this->header = $header;
$this->footer = $footer;
}
function load_views ($main, $data = [], $include_temps = true) {
if ($include_temps = true) {
$this->load->view('$this->template_dir.'/'.$this->header);
$this->load->view($main);
$this->load->view('$this->template_dir.'/'.$this->footer);
} else {
$this->load->view($main);
}
}
}
Then load it like: $this->load_views('login_view', $data);
You can do with library.
Create a new library file called template.php and write a function called load_template. In that function, use above code.
public function load_template($view_file_name,$data_array=array()) {
$ci = &get_instatnce();
$ci->load->view("header");
$ci->load->view($view_file_name,$data_array);
$ci->> load->view("footer");
}
You have to load this library in autoload file in config folder. so you don't want to load in all controller.
You can call
$this->template->load_template("index",$data_array);
If you want to pass date to view file, then you can send via $data_array
There is an article on this topic on ellislab forums. Please take a look. It may help you.
http://ellislab.com/forums/viewthread/86991/
Alternative way: Load your header and footer views inside the concern body view file. This way you can have batter control over files you want to include in case you have multiple headers and footer files for different purposes. Sample code shown below.
<html lang="en">
<head>
</head>
<body>
<div id="container">
<?php $this->load->view('header');?>
<div id="body">
body
</div>
<?php $this->load->view('footer');?>
</div>
</body>
</html>

How to load MVC views into main template file

I am working on my own MVC framework. Below is an example controller I have so far.
I have a way of loading models into my controller and also view files.
I am wanting to also have different template options for my site. My template will just be a page layout that inserts the views that are created from my controller into the middle of my template file.
/**
* Example Controller
*/
class User_Controller extends Core_Controller {
// domain.com/user/id-53463463
function profile($userId)
{
// load a Model
$this->loadModel('profile');
//GET data from a Model
$profileData = $this->profile_model->getProfile($userId);
// load view file and pass the Model data into it
$this->view->load('userProfile', $profileData);
}
}
Here is a basic idea of the template file...
DefaultLayout.php
<!doctype html>
<html lang="en">
<head>
</head>
<body>
Is the controller has data set for the sidebar variable, then we will load the sidebar and the content
<?php if( ! empty($sidebar)) { ?>
<?php print $content; ?>
<?php print $sidebar; ?>
If no sidebar is set, then we will just load the content
<?php } else { ?>
<?php print $content; ?>
<?php } ?>
</body>
</html>
Another Template without any header, footer, anything else, can be used for AJAX calls
EmptyLayout.php
<?php
$content
?>
I am looking for ideas on how I can load my main template file and then include and view files into the content area of my main layout file?
In the sample layout file, you can see that the content area has a variable called $content. I am not sure how I can populate that with the views content, to be inserted into my main layout template. If you have any ideas, please post sample
Something a little bit like
function loadView ($strViewPath, $arrayOfData)
{
// This makes $arrayOfData['content'] turn into $content
extract($arrayOfData);
// Require the file
ob_start();
require($strViewPath);
// Return the string
$strView = ob_get_contents();
ob_end_clean();
return $strView;
}
Then use with
$sidebarView = loadView('sidebar.php', array('stuff' => 'for', 'sidebar' => 'only');
$mainView = loadView('main.php', array('content' => 'hello',, 'sidebar' => $sidebarView);

Categories