• Czas czytania ~1 min
  • 24.08.2022

Auto Routes dla Laravel to pakiet od İzni Burak Demirtaş, który generuje trasy z kontrolera za pomocą automatycznego wykrywania. Działa poprzez wykrywanie publicznych metod na kontrolerze i generowanie dla nich tras:

// routes/web.php
Route::auto('/test', TestController::class);
 
//
// Controller example
//
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
 
class TestController extends Controller
{
    /**
     * URL: "/test" - main method
     */
    public function index(Request $request)
    {
        // controller code
    }
 
    /**
     * URL: "/test/foo-bar"
     * This method will only work with 'GET' method.
     */
    public function getFooBar(Request $request)
    {
        // controller code
    }
 
    /**
     * URL: "/test/foo_bar"
     * This method will only work with 'GET' method.
     */
    public function get_foo_bar(Request $request)
    {
        // controller code
    }
 
 
    /**
     * URL: "/test/bar-baz"
     * This method will only work with 'POST' method.
     */
    public function postBarBaz(Request $request)
    {
        // controller code
    }
}

trasaMetoda przyjmuje również trzeci argument do konfiguracji parametrów i oprogramowania pośredniczącego:

Route::auto('/test', TestController::class, [
    'name' => 'test',
    'middleware' => [YourMiddleware::class],
    // Parameters become available to methods
    'patterns' => [
        'id' => '\d+',
        'value' => '\w+',
    ],
]);
 
// Limit the methods that will define routes
Route::auto('/foo', 'FooController', [
    'only' => ['fooBar', 'postUpdatePost'],
]);

Podczas gdy większość programistów jest zadowolona z wbudowanych definicji routingu Laravela (routing Laravela jest tak dobry w mojej skromnej opcji ), jest to unikalny pomysł z przykładami w kodzie źródłowym pracy z odbiciem API PHP i routerem Laravel.Możesz dowiedzieć się więcej o tym pakiecie, uzyskać pełne instrukcje instalacji i wyświetlić kod źródłowy na GitHub.

Comments

No comments yet
Yurij Finiv

Yurij Finiv

Full stack

O

Professional Fullstack Developer with extensive experience in website and desktop application development. Proficient in a wide range of tools and technologies, including Bootstrap, Tailwind, HTML5, CSS3, PUG, JavaScript, Alpine.js, jQuery, PHP, MODX, and Node.js. Skilled in website development using Symfony, MODX, and Laravel. Experience: Contributed to the development and translation of MODX3 i...

O autorze CrazyBoy49z
WORK EXPERIENCE
Kontakt
Ukraine, Lutsk
+380979856297