CodeIgniter helper in the view - php

Is it a good workaround and would it be possible to use helper classes in the view, in CodeIgniter. I have a situation when I have to extract with regulars expression from a text couple of strings and generate outputs on matches. I would not like to do this directly in the view and I would like to use for this purpose a helper.
application
--view
---myview.php
and here I should call the helper and return results
for example I want to extract from the text the type of processor, than I pass the text and get returned the processor type. This one is needed because all the data in the view are generated by an API dynamically.
echo $myhelper->processor($text);

CodeIgniter's user guide explains that helpers can be loaded and their function used in views.
CodeIgniter does not load Helper Files by default, so the first step
in using a Helper is to load it. Once loaded, it becomes globally
available in your controller and views.
However it is not best pratice to load a helper in a view, so you could either auto-load the relevant helper, or load it in your controller(s).
A helper can be loaded anywhere within your controller functions (or
even within your View files, although that's not a good practice), as
long as you load it before you use it. You can load your helpers in
your controller constructor so that they become available
automatically in any function, or you can load a helper in a specific
function that needs it.
So, using helper functions in a view is fine, although it is encouraged that the helper is loaded in a controller, or auto-loaded.

get_instance()->load->helper('HELPER_NAME');

Just load the helper in your controller, then
$this->load->helper('MY_common_functions');
$template['content'] = $this->load->view('your_view');
In the view call your function name directly. In this case I called my convertor function
echo convertor($params);

It is standard in Codeigniter 4, always load Helper function before to use it, Either in Controller or Views.
In Codeigniter 4 If we declare Helper function in Controller's __construct method like:
<?php
namespace App\Controllers;
class NewsEventController extends BaseController{
public function __construct(){
helper('form');
}
Than this Helper function will available in all controller functions & views of that controller. Example a view file with 'form' helper function 'set_value()' in an input field like:
<input type="text" class="form-control" name="title" value="<?= set_value('title') ?>" >

Related

how to use another controller function without extends in our controller

how to use another controller function without extends in our controller
$this->load->library('../controllers/controllername');
already used
it is giving error =
Unable to locate the specified class: Session.php
Well you are not supposed to do that. If your controller uses repeatable logic, you should make class (Service for example), put the re-usable logic into it and call it in your controllers.
You can't use another controller function inside the controller. You can archive this in these two ways.
Create a Helper class
Create a generic model.

How to create and load new helper in CodeIgniter 4

i want to create new function in helper, but it still failed :
Call to undefined function
i save my helper at app/Helper/Text_helper.php using namespace App\Helpers;
and load helpers on BaseController using protected $helpers = ['text'];
Reference : https://codeigniter4.github.io/userguide/general/helpers.html#extending-helpers
but it's still not working
It's not mentioned in documents but remember to add a suffix _helper to filename of your helper otherwise it will not work in codeigniter 4.
For example if you have created a helper xxx.php, change it to xxx_helper.php.
To load a helper you can use helper function (Like: helper('xxx.php');) or add it to $helpers array that is an protected property in BaseController
If your idea is to "extend" (replace) a function on the stystem/helpers/text_helper note the lowercase in the name of the file, you have to respect it.
Also, the helper doesn't need a namespace... the helper loader will search for it.
The helper() method will scan through all PSR-4 namespaces defined in app/Config/Autoload.php and load in ALL matching helpers of the same name. This allows any module’s helpers to be loaded, as well as any helpers you’ve created specifically for this application. The load order is as follows:
app/Helpers - Files loaded here are always loaded first.
{namespace}/Helpers - All namespaces are looped through in the order they are defined.
system/Helpers - The base file is loaded last
the namespace will be used to load a helper on other location, for example:
helper('Libraries\MyFunctions');
as long as that path can be found through a namespace that has been set up within the PSR-4
Reference:
https://codeigniter4.github.io/userguide/general/helpers.html#extending-helpers
You need to load the helper into the app/Config/Autoload.php and still not work then please try to run composer dump-autoload

how to retrieve data in view Independent of controller in codeigniter

I have a sidebar in my site that receive some information from db and I can't use controller for retrieve data because I have different controller and same sidebar. How can I print this data in view page.
when I wrote in P.h.P code in the view it shows an error that it cant define variables.
How could I do this?
When you find that you need the same code in many different controllers a "custom library" (class) is the perfect choice. Documentation for creating your own libraries is found HERE.
Controllers should be using models to get data from the database. Custom libraries can also use models just like controllers. Here is a very basic custom library called Sidebar. It depends on a model (sidebar_model) that will not be shown. The purpose of the Sidebar library is to return the variables need by the sidebar_view file.
File: application/libraries/Sidebar.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Sidebar
{
protected $CI; // Read the documentation link to see why this is needed.
public function __construct()
{
$this->CI = & get_instance();
$this->CI->load->database(); //only needed if not already done
$this->CI->load->model('sidebar_model');
}
public function get_sidebar_data()
{
return $this->CI->sidebar_model->get_sidebar();
}
}
The library method get_sidebar_data() returns the variables for the view.
Here is a controller that uses the custom library. It will use the custom library and a view file (not shown) containing HTML for the sidebar.
File: application/controllers/Main.php
class Main extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('sidebar'); //can also be autoloaded
}
public function index()
{
$data['sidebar'] = $this->sidebar->get_sidebar();
$this->load
->view('banner')
->view('sidebar_view', $data)
->view('main_view')
->view('footer_view');
}
}
Any other controller that needs to show the sidebar would use this same pattern.
This controller loads four different view files and is using "method chaining" which is encouraged. Method chaining executes a tiny bit faster. But the best reason for using it? Less typing.
The method chaning could also be type like this:
$this->load->view('banner')->view('sidebar_view', $data)->view('main_view')->view('footer_view');
But, IMO, putting each ->view() on a separate line makes it easier to read.
You can create a helper for your common tasks. Then create a function for your sidebar and call it where you need it. Check this link for more details about creating helper
You can also create a library for it. Although it will not a very good choice.
create sidebar (view page) and Call model directly inside that sidebar (view page).
secondly call sidebar (view page) directly inside all other view pages.

How to call/use controller method in its View using Laravel 5.0?

I need to use my controller function in its own view.
I have tried Facade but failed.
You want to use a function defined in a Controller in one of the views it is calling?
If that's your question you can do following, (I am assuming the function is public static function returning some string). In your blade view file you can just call the function using the full name space path.
{{App\Http\Controllers\MyController::myFunction()}}
(There are ways in blade to import a class using use statement)
It is not recommended to call functions defined in controller like this (its against MVC), you can define them in the Model or create a separate Helper class.

How to pass data to layout in Laravel regardless of controller?

Let's say I got ControllerA and ControllerB which both implement the same layout. Now I want to pass data to layout, for example, a message that should appear in layout no matter which controller implements it. If I had only 1 controller, I would do something like:
class Controller extends \BaseController {
public function setupLayout() {
View::share('message', 'Hello world!');
}
// further methods
}
However, when I want multiple controllers to implement a layout, I have to do this in every controller, which doesn't sound reasonable. So I wanted to ask, is there any native way in Laravel to pass data to layout and not to copy code in every controller.
Thanks in advance!
For those cases I would recommend a view composer, where you can set them for more than one view (or layout) or just all of them:
View::composer(['store.index', 'products.*'], function($view)
{
$view->with('model', 'one');
$view->with('colour', 'black');
});
You can put that in your routes file, filters file or, like, me, create a app/composers.php and load by adding
require app_path().'/composers.php';
To your app/start/global.php.

Categories