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 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法