LaravelPHP

Cheat Sheet Laravel Bagian 3 ( Route dan URL )

Selamat siang para pembaca, beberapa hari lalu saya membagikan cheat sheet Laravel Database, kali ini lanjut cheat sheet bagian ke 3 yaitu Route dan URL, Route dan URL ini sering digunakan dalam membuat project menggunakan Laravel, langsung saja simak dibawah ini ;

Route

Route::get('foo', function(){});
Route::get('foo', 'ControllerName@function');
Route::controller('foo', 'FooController');

RESTful Controllers

Route::resource('posts','PostsController');
Route::resource('photo', 'PhotoController',['only' => ['index', 'show']]);
Route::resource('photo', 'PhotoController',['except' => ['update', 'destroy']]);

Triggering Errors

App::abort(404);
$handler->missing(...) in ErrorServiceProvider::boot();
throw new NotFoundHttpException;

Route Parameters

Route::get('foo/{bar}', function($bar){});
Route::get('foo/{bar?}', function($bar = 'bar'){});

HTTP Verbs

Route::any('foo', function(){});
Route::post('foo', function(){});
Route::put('foo', function(){});
Route::patch('foo', function(){});
Route::delete('foo', function(){});
RESTful actions
Route::resource('foo', 'FooController');
Registering A Route For Multiple Verbs
Route::match(['get', 'post'], '/', function(){});

Secure Routes(TBD)

Route::get('foo', array('https', function(){}));

Route Constraints

Route::get('foo/{bar}', function($bar){})
->where('bar', '[0-9]+');
Route::get('foo/{bar}/{baz}', function($bar, $baz){})
->where(array('bar' => '[0-9]+', 'baz' => '[A-Za-z]'))
Set a pattern to be used across routes
Route::pattern('bar', '[0-9]+')

HTTP Middleware

Route::get('admin/profile', ['middleware' => 'auth', function(){}]);

Named Routes

Route::currentRouteName();
Route::get('foo/bar', array('as' => 'foobar', function(){}));
Route::get('user/profile', [
  'as' => 'profile', 'uses' => 'UserController@showProfile'
]);
$url = route('profile');
$redirect = redirect()->route('profile');

Route Prefixing

Route::group(['prefix' => 'admin'], function()
{
  Route::get('users', function(){
      return 'Matches The "/admin/users" URL';
  });
});

Route Namespacing

Route::group(array('namespace' => 'Foo\Bar'), function(){})

Sub-Domain Routing

Route::group(array('domain' => '{sub}.example.com'), function(){});

URL

URL::full();
URL::current();
URL::previous();
URL::to('foo/bar', $parameters, $secure);
URL::action('NewsController@item', ['id'=>123]);
URL::action('FooController@method', $parameters, $absolute);
URL::route('foo', $parameters, $absolute);
URL::secure('foo/bar', $parameters);
URL::asset('css/foo.css', $secure);
URL::secureAsset('css/foo.css');
URL::isValidUrl('http://example.com');
URL::getRequest();
URL::setRequest($request);

Selesai, sekian cheatsheet laravel yang saya bagikan. Ke depan saya akan share lagi masalah-masalah code pemrograman yang saya alami dan penyelesaiannya. Siapa tau teman-teman para pembaca disini mengalami kasus yang sama dengan saya. Terimakasi dan semoga bermanfaat.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button