dpowt82802 2017-07-20 11:22
浏览 37

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

When i didn't find any best solution for auto route so i write my code.

Any suggestion will be appreciate.


In your routes.php file write this line at the end of the file

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

Now Create a new class in App\HTTP\Controllers

Routes.php (you can change name)

<?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

Now create your Classes in Controllers Folder and it will auto route...

Your file structure E.g:

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

        Post
          Post.php

        Banners
          Banners.php

        Folder
          File.php

Now you have idea, You can change logic according to your style, or you can use this it will work.

I am using Laravel 5.2

  • 写回答

1条回答 默认 最新

  • dqwh26750 2017-07-20 11:35
    关注

    As per my understanding, this could be the solution for you.

    Laravel has these built in methods for its controller

    <?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)
    {
        //
    }
    
    }
    

    Route can be defined using following syntax :

    <?php
    
    Route::resource('tests', 'TestsController');
    

    These actions will be handled:

    • 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
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度