{"flag":true,"single":true,"pageTitle":"routing","post":{"id":16,"user_id":"1","slug":"routing-6ekn","title":"routing","body":"<p><a href=\"https:\/\/laravel.com\/docs\/8.x\/routing\">Routing - Laravel - The PHP Framework For Web Artisans<\/a><\/p>\r\n<p><strong>Basic Routing:<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>Route::get('\/', function () {\r\n    echo \"working\";\r\n   \/\/ return 'Hello World';\r\n  \/\/ return view('welcome');\r\n});\r\n\r\nRoute::view('\/welcome', 'welcome', ['name' =&gt; 'Taylor']); \/\/also send array data<\/code><\/pre>\r\n<p><strong>Controller method access<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>use App\\Http\\Controllers\\UserController;\r\nRoute::get('\/user', [UserController::class, 'index']);\r\n\/\/OR\r\nRoute::get('\/user', 'UserController@index');<\/code><\/pre>\r\n<p><strong>Without use ..... user controller<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>Route::get('message',[App\\Http\\Controllers\\HomeController::class,'index']);<\/code><\/pre>\r\n<p><strong>NAMED ROUTES:<\/strong><\/p>\r\n<p>Named routes allows you to refer to the routes when generating URLs or redirects to the specific routes.<\/p>\r\n<pre class=\"language-markup\"><code>Route::get('home',[App\\Http\\Controllers\\HomeController::class,'index'])-&gt;name('rizwan');<\/code><\/pre>\r\n<pre class=\"language-markup\"><code>\/\/Generating URLs  \r\n$url= route('student_details');  \r\n\/\/Generating Redirects...  \r\nreturn redirect() -&gt; route('student_details');  <\/code><\/pre>\r\n<p>___________________________<\/p>\r\n<p><strong>Available Methods<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>Route::get($uri, $callback);\r\nRoute::post($uri, $callback);\r\nRoute::put($uri, $callback);\r\nRoute::patch($uri, $callback);\r\nRoute::delete($uri, $callback);\r\nRoute::options($uri, $callback);\r\n\r\nRoute::match(['get', 'post'], '\/', function () {  \/\/code }); \/\/multiple http verbs\r\n \r\nRoute::any('\/', function () {    \/\/code }); \/\/all http verbs<\/code><\/pre>\r\n<p><strong>Redirect&nbsp;<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>Route::redirect('\/here', '\/there');\r\n<\/code><\/pre>\r\n<h5><span style=\"font-size: 14pt;\">Route Parameters<\/span><\/h5>\r\n<pre class=\"language-markup\"><code>Route::get('\/user\/{id}', function ($id) { echo $id; });\r\nRoute::get('\/posts\/{post}\/comments\/{comment}', function ($postId, $commentId) { \/\/code });<\/code><\/pre>\r\n<p>If your route has <strong>dependencies<\/strong> that you would like the Laravel service container to automatically inject into your route's callback, you should list your route parameters after your dependencies:<\/p>\r\n<pre class=\"language-markup\"><code>use Illuminate\\Http\\Request;\r\nRoute::get('\/user\/{id}', function (Request $request, $id) {\r\n    return 'User '.$id;\r\n});<\/code><\/pre>\r\n<p><strong>Optional parameters<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>Route::get('\/user\/{name?}', function ($name = null) {\r\n    return $name;\r\n});<\/code><\/pre>\r\n<p><strong>Regular expressions<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>Route::get('\/user\/{name}', function ($name) {\r\n    \/\/\r\n})-&gt;where('name', '[A-Za-z]+');\r\n\/\/\r\nRoute::get('\/user\/{id}\/{name}', function ($id, $name) {\r\n    \/\/\r\n})-&gt;whereNumber('id')-&gt;whereAlpha('name');<\/code><\/pre>\r\n<p>https:\/\/laravel.com\/docs\/9.x\/routing#parameters-regular-expression-constraints<\/p>\r\n<p><strong>Route Groups:<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>Route::middleware(['first', 'second'])-&gt;group(function () {\r\n    Route::get('\/', function () { \/\/ });\r\n    Route::get('\/user\/profile', function () { \/\/  });\r\n});<\/code><\/pre>\r\n<pre class=\"language-markup\"><code>use App\\Http\\Controllers\\OrderController;\r\nRoute::controller(OrderController::class)-&gt;group(function () {\r\n    Route::get('\/orders\/{id}', 'show');\r\n    Route::post('\/orders', 'store');\r\n});<\/code><\/pre>\r\n<p>&nbsp;<\/p>","category_id":"2","is_private":"0","created_at":"2023-03-14T04:19:56.000000Z","updated_at":"2023-03-14T04:19:56.000000Z","category":{"id":2,"user_id":"1","name":"Laravel Core","slug":"laravel-nhyt","parent_id":"1","created_at":"2023-03-14T03:58:27.000000Z","updated_at":"2023-03-20T11:30:50.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"Routing - Laravel - The PHP Framework For Web Artisans Basic Routing: Route::get('\/', function () {     echo \"working\";    \/\/ return 'Hello  - routing (Updated: March 14, 2023) - Read more about routing at my programming site [SITE]","categories":[]}