tolik8
1/8/2019 - 11:25 PM

Laravel routes examples.php

<?php

//RouteServiceProvider.php
public function boot()
{
    //
    Route::pattern('id', '[0-9]+');
    app('router')->pattern('id', '[0-9]+');
    Route::patterns(['id' => '[0-9]+', 'cat' => '[A-Za-z]+']);
    parent::boot();
}

//routes/web.php
Route::get('/', function () {
    return view('welcome');
});

Route::get('/', ['as' => 'home', function () {
    return view('welcome');
}]);

Route::get('/page', function () {
    echo "Hello";
    return;
});

Route::get('/page', function () {
    echo "<pre>";
    print_r($_ENV);
    echo config('app.locale');
    echo Config::set('app.locale', 'ru');
    echo Config::get('app.locale');
    echo env('APP_NAME');
    echo "</pre>";
});

Route::post('/comments', function () {
    echo "<pre>";
    print_r($_POST);
    echo "</pre>";
});

Route::match(['get', 'post'], '/comments', function () {
    echo "<pre>";
    print_r($_POST);
    echo "</pre>";
});

Route::any('/comments', function () {
    echo "<pre>";
    print_r($_POST);
    echo "</pre>";
});

Route::get('/page/{id}', function ($id) {
    echo $id;
});

Route::get('/page/{id?}', function ($id = null) {
    echo $id;
});

/*Route::get('/page/{cat}/{id}', function ($var1, $var2) {
    echo $var1 . ' | ' . $var2;
});*/

Route::get('/page/{id}', function ($id) {
    echo $id;
})->where('id', '[0-9]+');

Route::get('/page/{cat}/{id}', function ($cat, $id) {
    echo $cat . ' | ' . $id;
})->where(['id' => '[0-9]+', 'cat' => '[A-Za-z]+']);

Route::group(['prefix' => 'admin/{id}'], function () {

    Route::get('page/create/{var}', function ($id) {

        return redirect()->route('home', ['id' => 25]);
        
        $route = Route::current();

        echo $route->getName();
        echo $route->getParameter('var', 24);
        print_r($route->parameters());

    })->name('createpage');

    Route::get('page/edit', function () {
        echo 'page/edit';
    });

});