dezsidog/laravel-change-way

change the dispatched controller by header

0.5.3 2018-05-22 02:50 UTC

This package is auto-updated.

Last update: 2024-10-17 20:14:38 UTC


README

License Build Status

change the dispatched controller by header.

bad english

will be tested soon.

feature

Rewrite Laravel controller dispatcher, so as to change action according to the version field in header.

install

composer require dezsidog/laravel-change-way

modify the base class in app/Http/Kernel.php from Illuminate\Foundation\Http\Kernel to Dezsidog\Http\Kernel

usage

write route entity

Route::get('test', "V1Controller@test");

Route::group(['version' => 'v2'], function(){
    Route::get('test', "V2Controller@test");
    Route::get('test2', "V2Controller@test2");
    Route::get('test3', "V1Controller@test");
});

create controller App\Http\Controllers\V1Controller.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class V1Controller extends Controller
{
    public function test()
    {
        return 'v1';
    }
}

create controller App\Http\Controllers\V2Controller.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class V2Controller extends Controller
{
    public function test()
    {
        return 'v2';
    }

    public function test2()
    {
        return 'v2';
    }
}

this package will according to the version field in header to determine use which test method.

public function testVersion()
    {
        $response = $this->getJson('api/test');
        
        $this->assertEquals('v1', $response->content());
        
        $response = $this->getJson('api/test',['version' => 'v2']);
        $this->assertEquals('v2', $response->content());

        $response = $this->getJson('api/test2', ['version' => 'v2']);
        $this->assertEquals('v2', $response->content());

        $response = $this->getJson('api/test2');
        $response->assertStatus(404);

        $response = $this->getJson('api/test3',['version' => 'v2']);
        $this->assertEquals('v1', $response->content());
    }