PHP code example of laralike / laralike-router

1. Go to this page and download the library: Download laralike/laralike-router 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/ */

    

laralike / laralike-router example snippets




use \laralike\LaralikeRouter as Route;

ky'; });

Route::get('/', function () { return 'hello milky'; });

Route::get('/foo', function () { return 'FOO'; });
Route::get('bar', function () { return 'BAR'; });

// returnされた値がarray型の場合は、
// `Content-Type: application/json`ヘッダを出力して、戻り値をjson_encodeして出力します。
Route::get('baz', function () { return ['BAZ']; });

Route::get('/', '\App\Controller\TestController@index');

Route::setNamespace('\\App\\Controller\\');
Route::get('/', 'TestController@index');

Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);

Route::match(['get', 'post'], '/', $callback);
Route::any('/', $callback);

Route::redirect('/here', '/there'); // 302
Route::redirect('/here', '/there', 301);
Route::permanentRedirect('/here', '/there'); // 301

Route::get('/here', function () { Route::runRedirect('/there'); }); // 302
Route::get('/here', function () { Route::runRedirect('/there', 301); });

Route::prefix('/foo')->group(function () {
	Route::redirect('/here', '/there'); // `/foo/here` => `/foo/there`
});

function view ($viewfile, $parameters) {
  $loader = new \Twig\Loader\FilesystemLoader(__DIR__ . '/');
  $twig = new \Twig\Environment($loader, [
    'debug' => true
  ]);
  return $twig->render($viewfile, $parameters);
}
Route::setView(view:class);
Route::prefix('/view')->group(function () {
  // 以下の様に使用するなら`Route::setView`は不要です。
  Route::get('/twig1', function () { return view('test.html.twig', ['name' => 'lala']); });
  // 以下の様ビュールートを使用する場合は`Route::setView`で予め設定する必要がある。
  Route::view('/twig2', 'test.html.twig', ['name' => 'milky']);
});

Route::get('user/{id}', function ($id) {
    return 'User '.$id;
});

Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
    //
});

Route::get('user/{name?}', function ($name = null) {
    return $name;
});

Route::get('user/{name?}', function ($name = 'lala') {
    return $name;
});

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

Route::get('user/{id}/{name}', function ($id) {
    //
})->where(['id' => '[0-9]+', 'name' => '[a-z]+']);

Route::prefix('/foo')->group(function () {
    // `/foo/bar`で`FOOBAR`と出力される
	Route::get('/bar', function () { echo 'FOOBAR' });
});

Route::middleware([function () { echo 'Hello '; }])
  ->prefix('/hello')
  ->group(function () {
    // `hello/lala`で`Hello lala`と出力される
    Route::get('/lala', function () { echo 'lala'; });
    // `hello/hikaru`で`Hello hikaru`と出力される
    Route::get('/hikaru', function () { echo 'hikaru'; });
});

// ネスト
Route::prefix('/hello')
  ->middleware([function () { echo 'Hello '; }])
  ->group(function () {
    Route::prefix('/cure')
      ->middleware([function () { echo 'Cure '; }])
      ->group(function () {
          // `hello/cure/milky`で`Hello Cure Milky`と出力される
          Route::get('/milky', function () { echo 'Milky'; });
      });
  });


Route::fallback(function () {
  // 404
});

  public static function defaultFallback()
  {
    header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
    echo '404 Not Found';
  }