Trying to understand the view method and general method calling in laravel - laravel

I am a new to laravel and trying to understand where the view method comes from and what mechanism allows it to be shown in the web.php folder in laravel.
For example :
Route::get('/', function () { return view('welcome'); })
I guess the view function is defined in some class. Bu which class is it and where is that class made reference to in order to access its method?
Thanks a lot if you can help me understanding this!

In most IDEs you can hold CTRL and left-click the function to view it's definition. view() is not defined in a class. It comes from a file called helpers.php.
This file is included at the beginning, so its functions can be used afterwards.
PHP is not only object oriented. Procedural and object oriented programming can be mixed together.

What I do usually in these cases is to search in the whole project (and remember to include vendor directory in your search) for: "function YOUR_FUNCTION_NAME" because somewhere in PHP there must be that function declared, whether is in a class or in a simple .php file.

view() method is a helper method inside src/Illuminate/Foundation/helpers.php. All the methods that declare here will be available everywhere inside Laravel application. You can check view() method here

Related

How to use $this in another file on Codeigniter?

I have a file named fn.php in my view, then I create a function to post data, like this
function post($input_name){
return $this->input->post($input_name);
}
In my controller I call it like this,
public function myFunc(){
$this->input->load('fn.php');
// then I use the function that I have created like this
post('myinputname');
}
But... I got error, how I can solve this problem? thank you so much
You should take some time to try and understand the MVC architecture. It is, after all, one of the main points of using a framework.
You can't put functions in a view and expect to load them somehow and access them. You can put functions in a model, controller, library or helper. In you case I would suggest a helper:
application/helpers/some_file_helper.php
function post($input_name){
$CI = &get_instance();
return $CI->input->post($input_name);
}
The get_instance() part is only used when $this (CI context) isn't available. This only happens in helpers and libraries. In views, controllers, and models $this is always available.
Model or controller:
$this->load->helper('some_file');
print_r(post('somevar'));
However if all you want to do is access the post variable use $this->input->post('somevar') directly and don't introduce an extra layer.

What is the difference of using URL::asset() and asset()?

I'm learning Laravel and want to understand it better. What is the difference of using URL::asset() and asset()?
Also what are the parts in front of the :: called?
Both of the functions are, the same. asset() is simply a helper function. Specifically, helpers are more appropriate for views. It's a preference thing.
The part before the :: is the class you are calling.
Generate a URL for an asset:
asset('img/photo.jpg');
Does the same as
URL::asset('img/photo.jpg');
Here you can read more about it: Laravel helpers
Actually asset() is a helper function which calls asset() method from the URL class, you can find that helper file (helpers.php) inside vendor/laravel/framework/src/Illuminate/Support folder and the function looks something like this (Version-4.2):
function asset($path, $secure = null)
{
return app('url')->asset($path, $secure);
}
There are other helper functions available in this file and those functions are very useful to use as shortcut ((less typing)) but behind the scene, when you call any helper function Laravel will call the original method from the class. You may also check the online documentation about helper functions.

CodeIgniter: what is the scope of load->helper

In a controller class I have this function:
public function index(){
$this->load->helper('url');
$data['title'] = 'News archive';
$this->load->view('news/index', $data);
}
I load helper url because I'm using anchor() in news/index. So seems like it's enough to load helper in the parent function, and I don't have to load it inside news/index.
So my question is what's going on underneath CI that lets me do this? Is load->view a function, or is it pasting the result of executing news/index on $data? How is load->view aware of helper url having been loaded in index? I'm still trying to make sense of how the CI framework works.
Also what would be the best place to load helper, in the constructor, or in each function as we need it?
Also what would be the best place to load helper, in the constructor, or in each function as we need it?
As a rough rule of thumb;
If you use the helper once in a controller - place it in that specific function
If you use the helper in multiple places in a controller - place it in that controllers constructor
If you use the helper in multiple places in multiple controllers - place it in the 'autoload' section once.
you can get all the answer if u go through the Loader Class in codeigniter...
path >> system/core/loader.php
everything that is done is here....
and for ut last question . according to the user guide
http://ellislab.com/codeigniter/user-guide/libraries/loader.html
loader, as the name suggests, is used to load elements. These elements can be libraries (classes) View files, Helpers, Models, or your own files.
so since it just loads the elements...
i usually(prefer) loading it in each function where needed. (unless i need the same elements in other functions too)

Using helper function inside a Core Class in CodeIgniter

There is a helper which I would like to use inside a core class, CI_Router (MY_Router, to be more accurate). In this custom router, I made some modifications to the original code, in order to be able to insert hyphens into my urls.
I have defined the helper on the autoload.php file, as usual, but it seems that I canĀ“t invoque a helper function inside a class other than a view or controller.
Any ideas about how to handle this? My initial approach was to use a helper, so I can reuse it on any place I want.
TYVM.
Helpers are not instantiated until after the core, thus why it does not work.
You will either have to:
Duplicate the function in your MY_Router class, or,
Rethink why you might be using the same function in the Router that you use in a standard controller or view.
Option 1 is obviously easier, but might not be preferable depending on how bad your OCD is.
You could try getting the instance of the main CI object and setting it to a variable, then load the helper using that. Ex:
$ci =& get_instance();
$ci->load->helper('date');
I know that works in other areas, not 100% sure about any of the router classes.

How to access function from every controller in codeigniter

Hi friends hi have this function in one controller , how can i access it from every other controller ?pls help
public function click_add($ads_id){
//some statement here
redirect($ads_site['url']);
}
There are a few possibilities:
Define a helper
Create a parent controller class from which all other controllers in your application extend
Create a library
Use ModularExtensions to allow calling one controller inside another
It all depends on what exactly the function should do. If it shouldn't access the model, then you could go for the first three options. Otherwise I'd suggest the latter two.
One solution would be to create your own library.

Resources