I am a little confused. The following is my nav controller. i have been getting my page layout this way.
nav
Home
Contact
Test
Main Controller
public function index()
{
if($this->uri->segment(3))
{
$content = $this->uri->segment(3);
}
else
{
$content = 'home_v';
}
$data = ['title'=> 'home', 'main_content' => "$content"];
$this->load->view('tempelate',$data);
}
since, all those content in my nav is inside view, its not a problem but what if i have a folder inside a view and want to send it to that controller? am i here all clear asking the question? because i am myself confused.
I want to sent another uri->segment into the same function.
Cat
category here is the folder name. so now what i basically want to add into the function is .
$folderName = $this->uri->segment(4);
Didn't understood much from the question. But still if you have a folder inside the view folder, and want to pass the $data to the view inside that folder, you can simply do,
$this->load->view('foldername/viewname', $data);
EDIT:
public function index()
{
$content = 'home_v';
if($this->uri->segment(3))
{
$content = $this->uri->segment(3);
}
$foldername = '';
if($this->uri->segment(4))
{
$foldername = $this->uri->segment(4).'/';
}
$data = ['title'=> 'home', 'main_content' => "$content"];
$this->load->view($foldername.$content, $data);
}
Related
I'm making an admin web with Codeigniter, but i encounter some problem and it can only triggered by removing "/" (only after hiding index.php) or adding "/" (without hiding index.php), the icon just missing and if i use an image from for profile pic the image will also missing.
*without hiding index.php and adding "/" on the url.
*after adding "/" on the last function name
here is my MY_Controller code to render page/view with template
class MY_Controller extends CI_Controller
{
function render_page($content, $data = NULL)
{
$data['head'] = $this->load->view('sliced/head', $data, TRUE);
$data['header'] = $this->load->view('sliced/header', $data, TRUE);
$data['js'] = $this->load->view('sliced/js', $data, TRUE);
$data['content'] = $this->load->view($content, $data, TRUE);
$data['sidebar'] = $this->load->view('sliced/sidebar', $data, TRUE);
$data['modal'] = $this->load->view('sliced/modal', $data, TRUE);
$data['footer'] = $this->load->view('sliced/footer', $data, TRUE);
$this->load->view('sliced/template', $data);
}
}
my controller
class Home extends MY_Controller {
public function __construct()
{
parent::__construct();
$this->load->model("user_model");
if($this->session->userdata('logged_in') !== TRUE){
redirect('login');
}
}
public function index()
{
$data["user"] = $this->user_model->getAll();
$data['controller']=$this;
$data['curruser'] = $this->session->userdata('name');
$data['role'] = $this->session->userdata('role');
$this->render_page("home/home", $data);
}
}
this is how i call images from assets folder on view
<img src="<?php echo base_url('assets/img/noprofimage.png')?>" alt="..." class="avatar-img rounded-circle">
i appreciate any help or reply.
Thank you.
Found the answer, and it was silly.
I just forgot using base_url to locate all the iconpack / images, guess i need to relax a bit hahaha.
Thanks to TimBrownlaw for suggesting the best way to using page source/info option.
And also thanks to imoverflow for making sure to check any links to my assets stuff.
I need a helper to change the default view of index page in wordpress.
My plugin is WPMVC generated and as per instructions in the official tutorial of WPMVC, i have created and loaded the helper but it is not working.
Can any one show me the right way to proceed?
Check the below link for a screen shot.
http://i.stack.imgur.com/3a1Ao.png
In the screeshot, the links and button below the records are added by me, overwriting the index file.
Now, i need to add a link near 'Edit | View | Delete' in the image, like,
Edit | View | Add Rule | Delete
Any suggestions on how to do that?
As i have told earlier, i created and loaded the helper but it is not functioning.
Need help. Thanks.
Codes:
/app/helpers/geozone_helper.php (Create Helper):
<?php
class GeozoneHelper extends MvcHelper {
public $_redirect_action = '';
public function __construct() {
if(empty($this->_redirect_action)) {
$this->_redirect_action = 'geozone_rules-add';
}
parent::__construct();
}
public function admin_actions_cell($controller, $object) {
$links = array();
$object_name = empty($object->__name) ? 'Item #'.$object->__id : $object->__name;
$encoded_object_name = $this->esc_attr($object_name);
$links[] = 'Edit';
$links[] = 'View';
$links[] = 'Add Rule';
$links[] = 'Delete';
$html = implode(' | ', $links);
return '<td>'.$html.'</td>';
}
}
/app/controllers/geozones_controller.php (load helper):
public function show() {
$object = $this->model->find_by_id($this->params['id'], array(
'includes' => array('Geozone')));
if (!empty($object)) {
$this->set('object', $object);
$this->render_view('show', array('layout' => 'public'));
}
$this->load_helper('geozone');
$this->set_object();
}
/app/view/geozones/show.php (link to the helper):
<h2><?php echo $object->__name; ?></h2>
<p>
<?php echo $this->html->link('← All Geozones', array('controller' => 'geozones')); ?>
<?php echo $this->geozone->admin_actions_cell($controller, $object->content); ?>
</p>
Thanks a lot again.
Issue resolved like this: The function
public function show() {
$this->load_helper('geozone');
$this->set_object();
}
in public controller is shifted to admin controller and function name is changed to index() like this:
public function index() {
$this->load_helper('geozone');
$this->set_objects();
}
Thanks for everyone who tried to solve this.
This is a custom CMS where menu list can be edited in backend and then need to be displayed in front end layout.
Menu controller is in -/application/modules/admin/controllers
and the code for render action is :
<?php
class Admin_MenuController extends CMS_Controller_AdminbaseController
{
public function renderAction()
{
$menu = $this->_request->getParam('menu');
$mdlMenuItems = new Model_MenuItems();
$menuItems = $mdlMenuItems->getItemsByMenu($menu);
if(count($menuItems)>0){
foreach($menuItems as $item){
$label = $item->label;
if(!empty($item->link)){
$uri = $item->link;
}else{
$uri = '/page/open/id/' . $item->pageId;
}
$itemArray[] = array(
'label' => $label,
'uri' => $uri
);
}
$container = new Zend_Navigation($itemArray);
$this->view->navigation()->setContainer($container);
}
}
}
When rendered in - /application/modules/admin/views/scripts/menu/render.phtml using
<? echo $this->navigation()->menu(); ?>
it renders fine, but instead I want to render it in /application/layouts/scripts.
Any help is much appreciated.
go to Zend_View_Helper create exapmle.php as an example;
on it insert function
public function abc()
{
//insert your code here
}
then go to layout.phtml and call it
echo $this->abc();
You can render it in layout this way <?= $this->action('render', 'menu', 'admin');?>
Additionally you can pass some parametrs in array.
Hope this helps you.
i have recently downloaded grocery from their site and i am having a small problem in loading views with a template ..
as in controller the function of loading view look like this
function _example_output($output = null)
{
$this->load->view('groceryView.php',$output);
}
function index()
{
$this->_example_output((object)array('output' => '' , 'js_files' => array() ,'css_files' => array()));
}
what i want to try to implement is this
$data['main_content'] = 'groceryView';
$this->load->view('dashboardTemplate/template',$data);
i dont know if i do this how can i pass output ... as it gives me an error if i do this
$data['output'] = 'output';
Well you can load multiple views . You need to modify _example_output. If you have header and footer seperately you could load it like this.
function _example_output($output = null)
{
$this->load->view('header.php');
$this->load->view('groceryView.php',$output);
$this->load->view('footer.php');
}
What's the best way for constructing headers, and footers? Should you call it all from the controller, or include it from the view file? I'm using CodeIgniter, and I'm wanting to know what's the best practice for this. Loading all the included view files from the controller, like this?
class Page extends Controller {
function index()
{
$data['page_title'] = 'Your title';
$this->load->view('header');
$this->load->view('menu');
$this->load->view('content', $data);
$this->load->view('footer');
}
}
or calling the single view file, and calling the header and footer views from there:
//controller file
class Page extends Controller {
function index()
{
$data['page_title'] = 'Your title';
$this->load->view('content', $data);
}
}
//view file
<?php $this->load->view('header'); ?>
<p>The data from the controller</p>
<?php $this->load->view('footer'); ?>
I've seen it done both ways, but want to choose now before I go too far down a path.
Actually, after researching this quite a bit myself, I came to the conclusion that the best practice for including headers and footers in MVC is a third option - namely extending a base controller. That will give you a little more flexibility than the text's suggestion, particularly if you're building a very modular layout (not just header and footer, also sidebar panels, non-static menus, etc.).
First, define a Base_controller class, in which you create methods that append your page elements (header, footer, etc.) to an output string:
class Base_controller extends Controller
{
var $_output = '';
function _standard_header($data=null)
{
if (empty($data))
$data = ...; // set default data for standard header here
$this->_output .= $this->load->view('header', $data, true);
}
function _admin_header($data=null)
{
if (empty($data))
$data = ...; // set default data for expanded header here
$this->_output .= $this->load->view('admin_header', $data, true);
}
function _standard_page($data)
{
$this->_standard_header();
$this->_output .=
$this->load->view('standard_content', $data, true);
echo $this->_output; // note: place the echo statement in a
// separate function for added flexibility
}
function _page_with_admin_header($data)
{
$this->_admin_header($data);
$this->_output .=
$this->load->view('standard_content', $data, true);
echo $this->_output;
}
}
Then, in your page controllers, simply extend the base class and call your functions to build the page.
class Page_controller extends Base_controller
{
function index()
{
$data = ...; // Set content data here
$this->_standard_page($data);
}
function admin()
{
$data = ...; // Set content and header data here
$this->_page_with_admin_header($data);
}
}
Using a base controller, you can achieve very clean code in your individual page controllers AND have separate views for elements on the page (allowing code reuse in both views and controllers). All you need to do is define your common page 'sections' (what you might be tempted to call 'fragments') as functions in your base controller.
And if the base controller should start to grow uncontrollably (which can happen on large sites), you can rearrange some of its less-general functions by placing them in subclasses and letting the corresponding page controllers extend those instead of the original base controller.
Enjoy!
You could also try it this way -- define a default view template, which then pulls in the content based on a variable ('content' in my example) passed by the controller.
In your controller:
$data['content'] = 'your_controller/index';
// more code...
$this->load->vars($data);
$this->load->view('layouts/default');
Then define a default layout for all pages e.g. views/layouts/default.php
// doctype, header html etc.
<div id="content">
<?= $this->load->view($content) ?>
</div>
// footer html etc.
Then your views can just contain the pure content e.g. views/your_controller/index.php might contain just the variables passed from the controller/data array
<?= $archives_table ?>
<?= $pagination ?>
// etc.
More details on the CI wiki/FAQ -- (Q. How do I embed views within views? Nested templates?...)
I think the first way you are doing it is cleaner. Simply from a point of view of knowledge that is going to be rendered. Rather than having to enter the view file to find the rest.
It's bad practice to call views inside of other views. This could be a form of controller view mixing. The view function in CI allows you to pass a third parameter that causes it to return that view's output as a string. You can use this to create a compound view.
For example:
class Page extends Controller {
function index() {
$data['page_title'] = 'Your title';
$this->load->view('default_layout', array(
'header' => $this->load->view('header' , array(), true),
'menu' => $this->load->view('menu' , array(), true),
'content' => $this->load->view('content', $data , true),
'footer' => $this->load->view('footer' , array(), true),
));
}
}
default_layout.php
<? echo $header, $menu, $content, $footer; ?>
You may want to combine your header and footer to make a template like this.
class Page extends Controller {
function index() {
$data['page_title'] = 'Your title';
$this->load->view('default_template', array(
'menu' => $this->load->view('menu' , array(), true),
'content' => $this->load->view('content', $data , true),
));
}
}
default_template.php
<html><head></head><body><span>Some Header HTML</span> // this is your header html
<? echo $menu, $content; ?>
<span>some footer HTML</span></body></html> // this is your footer html