Laravel自动路线像Codeigniter - 定制解决方案

时间:2017-07-20 11:22:50

标签: php laravel codeigniter routes

当我没有找到任何自动路线的最佳解决方案,所以我写了我的代码。

任何建议都会受到赞赏。

routes.php 文件中,在文件末尾写下此行

Route::match(["get","post"], '/{controller}/{method?}/{parameter?}', "Routes@index");

现在在App \ HTTP \ Controllers

中创建一个新类

Routes.php (您可以更改名称)

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;

class Routes extends Controller
{

public function index($controller,$method="",$parmeter=""){
    $controller = ucfirst($controller);

    if(empty($method)){
        // e.g: example.com/users 
        // will hit App\Http\Controllers\Users\Users.php Class Index method
        return \App::call("App\Http\Controllers\\$controller\\$controller@index");          
    }



    // e.g: example.com/users/list
    // will hit App\Http\Controllers\Users\Users.php Class List method
    // If user.php has List method then $parameters will pass
    $app = \App("App\Http\Controllers\\$controller\\$controller");
    if(method_exists($app, $method)){
        return $app->$method($parmeter);
    }



    // If you have a folder User and have multiple class in users folder, and want to access other class
    // e.g: example.com/users/groups
    // will hit App\Http\Controllers\Users\Groups.php Class Index method
    $method = ucfirst($method); //Now method will be use as Class name
    $app = \App("App\Http\Controllers\\$controller\\$method");
    return $app->index();
}

}
  

DONE

现在在Controllers文件夹中创建您的类,它将自动路由...

您的文件结构E.g:

App
   HTTP
     Controllers
        Users
          Users.php
          Groups.php
          Etc.php

        Post
          Post.php

        Banners
          Banners.php

        Folder
          File.php

现在你有了想法,你可以根据自己的风格改变逻辑,或者你可以使用它来运作。

我使用的是Laravel 5.2

1 个答案:

答案 0 :(得分:0)

根据我的理解,这可能是您的解决方案。

Laravel为其控制器提供了这些内置方法

<?php

class TestsController extends BaseController {

/**
 * Display a listing of the resource.
 *
 * @return Response
 */
public function index()
{
    //
}

/**
 * Show the form for creating a new resource.
 *
 * @return Response
 */
public function create()
{
    //
}

/**
 * Store a newly created resource in storage.
 *
 * @return Response
 */
public function store()
{
    //
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function show($id)
{
    //
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function edit($id)
{
    //
}

/**
 * Update the specified resource in storage.
 *
 * @param  int  $id
 * @return Response
 */
public function update($id)
{
    //
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return Response
 */
public function destroy($id)
{
    //
}

}

可以使用以下语法定义路径:

<?php

Route::resource('tests', 'TestsController');

将处理这些行动:

  • GET / tests tests.index
  • GET / tests / create tests.create
  • POST / tests tests.store
  • GET / tests / {id} tests.show
  • GET / tests / {id} / edit tests.edit
  • PUT / PATCH / tests / {id} tests.update
  • DELETE / tests / {id} tests.destroy