PHP code example of calject / lann-route

1. Go to this page and download the library: Download calject/lann-route library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

calject / lann-route example snippets




/**
 * Class UploadController
 * @package App\Http\Controllers\Annotation
 */
Route::group(["prefix" => "file", "namespace" => "Annotation"], function () {
    // 文件上传
    Route::post('upload', 'UploadController@upload');
    // 文件下载
    Route::get('download', 'UploadController@download');
});

/**
 * Class TestController
 * @package App\Http\Controllers\Annotation
 */
Route::group(["prefix" => "annotation", "middleware" => ["api"], "namespace" => "Annotation"], function () {
    Route::get('getPath', 'TestController@getPath');
    Route::post('setPath', 'TestController@setPath');
    Route::match(["get", "post", "put", "delete"], 'user', 'TestController@user')->name('user');
    Route::match(["get", "post", "put", "delete"], 'file', 'TestController@file');
    Route::match(["get", "post", "put", "delete"], 'article', 'TestController@article');
});

    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        $this->mapApiRoutes();

        $this->mapCreditRoutes();

        $this->mapDevelopRoutes();
        
        $this->mapTestRoutes();
        
        // 添加注解实现
        $annotationRoute = new AnnotationRoute();
        // $annotationRoute->envs('local');                 // 设置生效环境
        // $annotationRoute->envs(['local', 'develop']);    // 设置生效环境
        $annotationRoute->register(...);                    // 注册:实现Calject\LannRoute\Contracts\AnnotationTagInterface接口的tag类
        $annotationRoute->mapRefRoutes();
    
    }



namespace App\Http\Controllers\Annotation;

use App\Http\Controllers\Controller;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

/**
 * Class TestController
 * @package App\Http\Controllers\Annotation
 * @group(prefix='annotation', middleware='api')
 * @restful()
 */
class TestController extends Controller
{
    /**
     * @param Request $request
     * @return ResponseFactory|Response
     * @uri('testA')
     * @name(testA)
     */
    public function testA(Request $request)
    {
        return response('testA');
    }
    
    /**
     * @param Request $request
     * @return ResponseFactory|Response
     * @get('testB')
     */
    public function testB(Request $request)
    {
        return response('testB');
    }
    
    /**
     * @param Request $request
     * @return ResponseFactory|Response
     * @prefix('ttt')
     * @uri('testC, testC2')
     */
    public function testC(Request $request)
    {
        return response('testB');
    }
    
    /**
     * @param Request $request
     * @return ResponseFactory|Response
     * @api(uri='testD', method='post', middleware='test,td')
     */
    public function testD(Request $request)
    {
        return response('testB');
    }
    
    /**
     * @param Request $request
     * @return ResponseFactory|Response
     * @uri(testE)
     * @name(testE)
     * @middleware(query)
     */
    public function testE(Request $request)
    {
        return response('testD');
    }
}



namespace Calject\LannRoute\Components\Tag;

use Calject\LannRoute\Components\Model\RouteClassData;
use Calject\LannRoute\Components\Model\RouteFuncData;
use Calject\LannRoute\Constant\RouteConstant;
use Calject\LannRoute\Contracts\AbsAnnotationTag;

/**
 * Class Route
 * @package Calject\LannRoute\Components\Tag
 * @example @route(xxx='xxx',xxx='xxx',...)
 */
class Route extends AbsAnnotationTag
{
    /**
     * @return string
     */
    public function tag(): string
    {
        return 'route';
    }
    
    /**
     * @param RouteFuncData|RouteClassData $route
     * @param array|string $params
     * @return mixed
     */
    public function handle($route, $params)
    {
        $this->doRoutes($route, $params);
    }
    
    /**
     * tag过滤参数(数组或者为空)
     * @return array|mixed|null|void
     */
    public function tagParams()
    {
        return ['middleware', 'uri', 'method'];
    }
    
    /**
     * 作用范围, 可选: class, function
     * @return array|string
     */
    public function scope()
    {
        return RouteConstant::SCOPE_FUNCTION;
    }
}

// 添加注解实现
$annotationRoute = new AnnotationRoute();
$annotationRoute->register(new Tag());
$annotationRoute->mapRefRoutes();



namespace App\Http\Controllers\Annotation;

use App\Http\Controllers\Controller;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

/**
 * Class TestController
 * @package App\Http\Controllers\Annotation
 * @group(prefix='annotation', middleware='api')
 */
class TestController extends Controller
{
    /**
     * @param Request $request
     * @return ResponseFactory|Response
     * @route(uri='test', method='get')
     */
    public function test(Request $request)
    {
        return response('test');
    }
}



/**
 * Class TestController
 * @env(produce,develop,local)  限定produce,develop,local环境生效
 * @file(route.php)
 */
class TestController extends Controller
{
    /**
     * @return ResponseFactory|Response
     * @route(uri='test', method='get')
     * @env(local) 限定local生效
     */
    public function test()
    {
        return response('test');
    }
}

method=get,post,put,delete
prefix=annotation
middleware=api
...