CodeIgniter - Including sections of code? - codeigniter

I have about 9 lines of code that will be the same in many of my controllers. They go in the index and are used to check whether the user is logged in, what level of access has been granted, and grab some session variables. What's the best way to include snippets of code that are not complete functions within themselves?
The snippets are not in all controllers, just the ones that build the admin section of the application.
It would be great if I could use one line of code to include the snippet but I'm new to CodeIgniter and I don't want to stray from best practices. If you could include a brief example that would help me visualize this. Thanks!

You want to create an extension of the base CI controller and do your checks there. Extending the cores are in the documentation here: http://ellislab.com/codeigniter/user_guide/general/creating_libraries.html Scroll down to the Extending Native Libraries part.
In your new controller you can do something like this:
<?PHP
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct(); //Gets all the CI_Controller functions and makes them available to this controller.
if($this->session->userdata('is_logged_in')
{
$this->load->model('categories');
$this->categories=$this->categories->getAllCategories();
}
}
}
Then in your other controllers you use MY_Controller instead of CI_Controller like this:
<?php
class Pages extends MY_Controller {
Obviously you'll have your own checks and variables to load but this should give you the general idea. In the example above I can now use $this->categories to retrieve my categories anywhere in the application that's been loaded with MY_Controller.

I'm using a 'main' template file (views/template.php):
// views/template.php
<?php $this->load->view('includes/header'); ?>
<?php $this->load->view('includes/' . $main_content); ?>
<?php $this->load->view('includes/sidebar'); ?>
<?php $this->load->view('includes/footer'); ?>
So the view directory structure is something like this:
--views
--includes
- header.php
- footer.php
- template.php
As you can see inside the view/includes/ folder I'm using some reuseable snippets like 'header.php', 'footer.php'...
So inside a controller I'm loading this main template file:
class MyController extends CI_Controller {
public function index()
{
//...
$this->load->view('template',$data);
}
}
So it loads 'template.php'.
And can use the $data inside header.php, footer.php too..
But you can vary this as you want, it's just a 'basic' idea..

Related

To link in CodeIgniter within application

<a class="navbar-brand" href="<?php echo base_url ('welcome/aboutus');?> "> About Us </a>
Here welcome is controller name and aboutus is page name. I have set autoload. If I run this then it shows "Object not Found", what's wrong with that?
Your question is a bit hard to help you with because you have limited code to go by.
You may need to set your base url
$config['base_url'] = 'http://localhost/yourproject/';
$config['index_page'] = '';
Welcome is the controller name as you said and aboutus would be a function name
Follow the PHP Codeigniter Naming Style Guide
Welcome.php
<?php
class Welcome extends CI_Controller {
public function index() {
}
public function aboutus() {
}
}
You may need a suitable .htaccess file to remove it so can work with out index.php
application
system
.htaccess
index.php
Try with <?php echo base_url ('index.php/welcome/aboutus');?>
In CodeIgniter, it dose not work like that.
<?php base_url ('welcome/aboutus') ?>
What it means is that Welcome is the name of the controller and aboutus is the name of the function in the controller.
So when you click on the link generated by the above code, it will search for a function named aboutus in the controller file Welcome.
To load about us using your link, the controller should look like this,
<?php
class Welcome extends CI_Controller {
public function index() {}
public function aboutus() {
$this->load->view('Name of the view file');
}
} ?>
I think you should know What is MVC Framework? and How Dose it Work?.
Have a look at these::
CI MVC Framework
CI MVC Static Page

Laravel 5 How to dynamically include External CSS and JS file depends on controllers/routes

I'm wondering what could be a good way to
dynamically include a js file in my main blade
template like this
<?php
$currentRoute = Route::currentRouteName();
$currentRoute = $currentRoute?$currentRoute:'home';
$currentRoute = explode('.',$currentRoute);
?>
<script src="{{$currentRoute[0]}}.js"></script>
but just a less tricky :)
I did something like that in that past.
Here, is my "trick" to do this.
First you need a top level controller class
class MainController extends Controller {
protected static $js_files = [];
protected static $css_files = [];
public static function boot()
{
view()->share('js_files', static::$js_files);
view()->share('css_files', static::$css_files);
  parent::boot();
}
}
Then, all of your controllers must inherit from your MainController class.
The reason I use the keyword "static::" instead of "self::" is because I want to retrieve the latest child properties and not the "MainController" property, which would gave me the keywords "self::".
See http://php.net/manual/en/language.oop5.late-static-bindings.php
In your blade layout, in the head section do the following :
#foreach($css_files as $src)
<link href="{{$src}}" rel="stylesheet">
#endforeach
and before your closing body tag
#foreach($js_files as $src)
<script src="{{$src}}"></script>
#endforeach
In each controllers, you have to put in the $js_files and $css_files inherited properties the sources of all of your js/css.
if you want to include a JS/CSS only for one route, you can add the source of the file directly in the method.
And voilà !

CodeIgniter Dry Navigation

My boss told me make dry navigation dont use repetitive code, for navigation i am trying to extend CI_Controler and with construct load header nav, body, footer files.
My question is when i create new controller and when i try to load different view files, how to achive that???
my extended controler
class MY_Controller extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->load->view('view_header');
$this->load->view('includes/nav_home');
$this->load->view('view_home');
$this->load->view('view_footer');
}
}
and later when i am creating new controler how to load diffrent view files
class Pages extends MY_Controller
{
public function __construct() {
$this->load->view('includes/nav_new_view');
$
}
}
You can create a template library yourself. For example :
function template($view_name,$view_data){
//below will return html string from view name
$data['content'] = $this->load->view($view_name,$view_data,true)
//load main template view and pass the html string to main template
$this->load->view('main_template',$data);
}
In main template, just echo $content
If I understand your question, you're trying to achieve a template situation. For this, the best way is to actually call your templates view files within a primary page view. What I mean is your controller function (not the constructor, an actual class function representing a page) should call a primary view such as
$this->load->view('page1', $this->data);
and within that file, you call
$this->load->view('nav', $this->data);
then your content and then
$this->load->view('footer', $this->data);
You would then repeat the process for page 2 where in your controller's page2 function, you would call
$this->load->view('page2', $this->data);
and your page2 view file is almost identical to page1 except you use your page 2 content in that area.
You could even use a single template view file and pass it a $content variable (which obviously changes per page) and call
$this->load->view('template', $this->data);

CodeIgniter - Autoload for views?

is there any way to load a view (for header or footer) on every function (in a controller)? I have a couple of if/else statements there and it would be a pain to change it all when I'll need to.
Yes, you can load the view in the __construct function at the top of your controller. Take
a look at the PHP manual on Constructors
function __construct()
{
parent::__construct();
$this->load>-view('your_view');
}
If the header and footer are going to be constant and required components for the visual part of your site, but you may want to load a different content portion between your header and footer, then you can make a function that will take an argument.
private function doViews($argument)
{
$this->load->view('header');
$this->load->view($argument);
$this->load->view('footer');
return NULL;
}
You may want to have an array of available views inside the doViews function in order to do proper validation that the file exists. Then you simply call the function in each method in your controller like this:
$this->doViews('main_content');
You should try using a Template library like this: https://github.com/philsturgeon/codeigniter-template
Then all you need to is put this in the controller (can be in __construct or within a method)
$this->template->set_partial('header', 'layouts/header');
$this->template->set_partial('footer', 'layouts/footer');
$this->template->set_partial('sidebar', 'layouts/sidebar');
Then to send data like you do with a view:
$this->template->build('create', $this->data);
you could create your main_view ... as a master page that already has a structure:
main_view.php
$this->load>-view('header');
<?php //get content here
?>
$this->load>-view('footer');
If you want to change something in the header or footer (through a statement) you can add content:
function __construct()
{
parent::__construct();
$data['footer'] = ($a == 'foo') ? 'footer one' : 'footer two';
$data_to_main = $this->load->view('template/footer', $data, TRUE);
$data_to_main = 'others';
$this->load>-view('main_view', $data_to_main);
}

Magento: how to add a link via code to a block of a custom module?

I'm trying to add a link to one of my blocks to a specific action of one of my controllers. Looking through the class docs and googling didn't resolve something useful. (maybe I just used the wrong search queries).
My Controller has two actions:
indexAction() and exportAction()
Now, in one of my blocks I wand to add link to exportAction(). I've found the method addLink() but this doesn't work.
Maybe anyone knows how to do that ? Or could point me to the right resources on the net ?
Regards, Alex
Block Example:
<?php
class Polyvision_Tempest_Block_Adminhtml_View extends Mage_Adminhtml_Block_Template
{
public function __construct()
{
parent::__construct();
}
protected function _toHtml()
{
$html = "whatever";
return $html;
}
}
?>
Your question isn't clear/complete.
A block renders HTML, either through a phtml template or through PHP code. To add an HTML link, you just render a html anchor tag with an href
//via PHP
protected function _toHtml()
{
$html = 'My Link';
return $html;
}
//via phtml template
#your block
class Polyvision_Tempest_Block_Adminhtml_View extends Mage_Adminhtml_Block_Template
{
protected function _construct()
{
$this->setTemplate('path/to/from/template/folder/as/basetemplate.phtml');
}
}
#your template
My Link';
The addLink method is a special method that only applies to certain types of blocks. When you call it it add links information to the block's data properties. Then, it's _toHtml method or phtml template has been written such that it loops over the stored data to output links. It doesn't apply to general blocks, which is what makes your question confusing.
Hope that helps!

Resources