Laravel 5,如何运行根据URL收到的具体方法
Laravel 5, how to run a specific method according to the URL received
在我的路由文件中,我有一堆或用于测试目的的路由:
/** testing controllers */
Route::get('viewemail', 'TestController@viewemail');
Route::get('restore', 'TestController@restore');
Route::get('sendemail', 'TestController@send_email');
Route::get('socket', 'TestController@socket');
Route::get('colors', 'TestController@colors');
Route::get('view', 'TestController@view_test');
Route::get('numbers', 'TestController@numbers');
Route::get('ncf', 'TestController@ncf');
Route::get('dates', 'TestController@dates');
Route::get('print', 'TestController@printer');
Route::get('{variable}', 'TestController@execute');
/** End of testing controllers */
我想消除所有这些路由并简单地使用给定的名称 URL 来调用和 return 方法:
我是这样完成的:
Route::get('{variable}', 'TestController@execute');
在我的测试控制器中:
public function execute($method){
return $this->$method();
}
基本上我想知道 Laravel 是否有内置解决方案来执行此操作,我正在阅读文档但找不到任何方法来完成此操作。
来自官方文档:
http://laravel.com/docs/5.1/controllers#implicit-controllers
Laravel allows you to easily define a single route to handle every
action in a controller class. First, define the route using the
Route::controller
method. The controller method accepts two
arguments. The first is the base URI the controller handles, while the
second is the class name of the controller:
Route::controller('users', 'UserController');
Next, just add methods to your controller. The method names should begin with the
HTTP verb they respond to followed by the title case version of the
URI:
<?php
namespace App\Http\Controllers;
class UserController extends Controller
{
/**
* Responds to requests to GET /users
*/
public function getIndex()
{
//
}
/**
* Responds to requests to GET /users/show/1
*/
public function getShow($id)
{
//
}
/**
* Responds to requests to GET /users/admin-profile
*/
public function getAdminProfile()
{
//
}
/**
* Responds to requests to POST /users/profile
*/
public function postProfile()
{
//
}
}
As you can see in the example above, index methods will respond to the
root URI handled by the controller, which, in this case, is users.
您可以为要侦听的端点添加路由模式。将它们路由到控制器操作,然后检查请求:
class TestController extends Controller
{
public function handle(Request $request)
{
$method = $request->segment(1); // Gets first segment of URI
// Do something…
}
}
并在您的路线服务提供商中:
$router->pattern('{variable}', 'foo|bar|qux|baz');
在我的路由文件中,我有一堆或用于测试目的的路由:
/** testing controllers */
Route::get('viewemail', 'TestController@viewemail');
Route::get('restore', 'TestController@restore');
Route::get('sendemail', 'TestController@send_email');
Route::get('socket', 'TestController@socket');
Route::get('colors', 'TestController@colors');
Route::get('view', 'TestController@view_test');
Route::get('numbers', 'TestController@numbers');
Route::get('ncf', 'TestController@ncf');
Route::get('dates', 'TestController@dates');
Route::get('print', 'TestController@printer');
Route::get('{variable}', 'TestController@execute');
/** End of testing controllers */
我想消除所有这些路由并简单地使用给定的名称 URL 来调用和 return 方法:
我是这样完成的:
Route::get('{variable}', 'TestController@execute');
在我的测试控制器中:
public function execute($method){
return $this->$method();
}
基本上我想知道 Laravel 是否有内置解决方案来执行此操作,我正在阅读文档但找不到任何方法来完成此操作。
来自官方文档: http://laravel.com/docs/5.1/controllers#implicit-controllers
Laravel allows you to easily define a single route to handle every action in a controller class. First, define the route using the
Route::controller
method. The controller method accepts two arguments. The first is the base URI the controller handles, while the second is the class name of the controller:Route::controller('users', 'UserController');
Next, just add methods to your controller. The method names should begin with the HTTP verb they respond to followed by the title case version of the URI:
<?php namespace App\Http\Controllers; class UserController extends Controller { /** * Responds to requests to GET /users */ public function getIndex() { // } /** * Responds to requests to GET /users/show/1 */ public function getShow($id) { // } /** * Responds to requests to GET /users/admin-profile */ public function getAdminProfile() { // } /** * Responds to requests to POST /users/profile */ public function postProfile() { // } }
As you can see in the example above, index methods will respond to the root URI handled by the controller, which, in this case, is users.
您可以为要侦听的端点添加路由模式。将它们路由到控制器操作,然后检查请求:
class TestController extends Controller
{
public function handle(Request $request)
{
$method = $request->segment(1); // Gets first segment of URI
// Do something…
}
}
并在您的路线服务提供商中:
$router->pattern('{variable}', 'foo|bar|qux|baz');