Looking at Laravel code I found they are passing variable from 'routes' to 'views' using the following method:
$arraysan = ['mike','robert','john']; **//Variable to be passed**
return view('home')->withArraysan($arraysan); **//Variable passed with** name "withArraysan"
In that above syntax they call a function named withArraysan
which doesn't exist.
Can somebody explain how its been handled in Laravel?
For a while now, PHP has had the concept of magic methods
- these are special methods that can be added to a class to intercept method calls that do not exist.
It appears that Laravel Views implement __call
- this then intercepts a call to an undefined method on the object, and is passed both the name of the method being called as well as the arguments. In this way, the View object can then see that the withArraysan
call began with
and call the concrete method with
, passing the second part Arraysan
as the first argument, and the argument to withArraysan
as the second part.
If I've got your question then in Laravel they had a class View
using magic method __call
to handle the above function and the code for that function is like as follows
public function __call($method, $parameters)
{
if (Str::startsWith($method, 'with')) {
return $this->with(Str::snake(substr($method, 4)), $parameters[0]);
}
throw new BadMethodCallException("Method [$method] does not exist on view.");
}
And you can find this within
your_project_folder/vendor/laravel/framework/src/Illuminate/View/View.php
$arraysan = ['mike', 'robert', 'john']; // Variable to be passed
return view('home')->with('AnyVariable', $arraysan);
Try this! This will work.
Also check in home.blade.php,
<?php
print_r($AnyVariable);die;
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.