How do I implement a non-fixed number of parameters in a Laravel route ?, Laravel route

Source: Internet
Author: User

How do I implement a non-fixed number of parameters in a Laravel route ?, Laravel route

Preface

Laravel is a good framework. I am also studying and using it and promoting it in my company. I recently found a particularly interesting piece of code while reading the Laravel Source Code. For details, refer:

... What are these three points used? I checked the PHP manual and found that this is a list of variable parameters.

What is this? The PHP manual explains this.

... Is a list of parameters that can be changed in user-defined functions.

... Exists in PHP 5.6 and later versions. Use functions in PHP 5.5 and earlier versions func_num_args(),func_get_arg() , Andfunc_get_args().

Variable Number Parameter List, which may be abstract.

We can understand that we have customized a function or a function, but the number of parameters of this function is not fixed, that is, the list of variable parameters.

For the variable quantity parameter list, let's look at two examples;

<?phpfunction sum(...$numbers) { $acc = 0; foreach ($numbers as $n) {  $acc += $n; } return $acc;}echo sum(1, 2, 3, 4);?>

The above routine will output:

10

The variable quantity parameter is passed to the function, and the given parameter variable is used as an array.

Let's look at another example:

<?phpfunction add($a, $b) { return $a + $b;}echo add(...[1, 2])?>

The above routine will output:

3

The variable quantity parameter is passed to the function, and the given array is used as the parameter variable.

What is the relationship between this variable quantity parameter and Laravel routing?

In Laravel, custom routing is very free, for example:

Route: get ('user/{id} ', 'userscontroller @ filter'); // public function filter ($ id) {# code ...}

It may be like this:

Route: get ('user/{id}/{name} ', 'userscontroller @ filter'); // public function filter ($ id, $ name) {# code ...}

In Laravel routing, how does one implement such a number of parameters in the code? The variable quantity parameter is used.

// */vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php/** * Dispatch a request to a given controller and method. * * @param \Illuminate\Routing\Route $route * @param mixed $controller * @param string $method * @return mixed */public function dispatch(Route $route, $controller, $method){ $parameters = $this->resolveClassMethodDependencies(  $route->parametersWithoutNulls(), $controller, $method ); if (method_exists($controller, 'callAction')) {  return $controller->callAction($method, $parameters); } return $controller->{$method}(...array_values($parameters));}

I have to admire Laravel's author Taylor naoluqing!

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.