繁体   English   中英

像代码点火器一样的laravel路由

[英]laravel routing like code igniter

我试图从使用代码点火器切换到laravel然而我注意到在我遵循的每个教程中,我们总是在laravel中的route.php中声明路由,这与代码点火器不同,它具有默认路由,如http://localhost/projname/controller/method 有没有办法像CI这样的自动路由,或者我只是错过了laravel路由规则中的一些内容?这非常重要,因为我们都知道大型网站有超过50个链接,如果我们要宣布它将是一个喧嚣那些都在laravel的routes.php中。

有没有办法像CI这样的自动路由

为什么有。 在你的路由文件中做Route::controller(Controller::detect());

现在在您的控制器类中确保每个函数名称与action_连接。 因此,如果您的函数名称是homepage()那么将其action_homepage()

请记住,您可以使用restful控制器名称get_homepage()post_homepage() 但是你必须在控制器中声明这个类变量public static $restful = true;

    //create controller name like UserController

//Steps:
// 1. route matched to either post or get request, 
// 2. used web/controllerName/MethodName/Parameter1/parameter2 .. 
// all parameters received in an array as $params. web/ is used like a route 
// prefix. If no method is passed it will call index method
// 3. explode the parameter
// 4. called the controller with method and paramteres passed
// 5. parameters are matched for regex allowing alphanumeric and slash (url)
// 6. passed through guest middleware

// created controller as mentioned below:

// class SomeController extends Controller
// {
//     public function index($param1,$param2,$param3){

//       return 'index'.$param1.$param2.$param3;
//     }


// }



Route::match(['get','post'],'/web/{controller}/{method?}/{params?}', function ($controller, $method='index', $params='') {    

    $params = explode('/', $params);

    $controller = app()->make("\App\Http\Controllers\\". ucwords($controller).'Controller' );


    return $controller->callAction($method, $params);

})->where('params', '[A-Za-z0-9/]+')->middleware('guest');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM