donqh00404 2015-10-08 15:49
浏览 89
已采纳

Laravel 5,如何根据收到的URL运行特定方法

In my routes files I have a bunch or routes for testing purposes:

/** 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 */ 

I want to eliminate all those routes and simple use the name of the given URL to call and return the method:

I have accomplished that in this way:

Route::get('{variable}', 'TestController@execute');

And in my testing controller:

public function execute($method){
    return $this->$method();
}

Basically what I want to know if Laravel has a built in solution to do this, I was reading the documentation but couldn't find any way to accomplish this.

  • 写回答

2条回答 默认 最新

  • doutenggu4070 2015-10-08 16:20
    关注

    From official documentation: 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号