-2

I have this code in routes, is it possible to simplify it? Thanks

Route::get('/post1', function () { 
    return view("post1");
})->name("/post1");
Omar GB
  • 29
  • 6
  • 1
    There's no complexity in this code what's the purpose of simplify what's already simple? – Elias Soares May 09 '22 at 23:25
  • i would suggest reading the routing documentation again, there is a method to create a route definition that just returns a view ... `Route::view` ... also if you are looking for 'code review' type of stuff or 'rewrite my code' things there are other sites on this network for that – lagbox May 09 '22 at 23:26
  • 2
    you could use `Route::view('post1', 'post1')`. more here https://laravel.com/docs/9.x/routing#view-routes – usrNotFound May 10 '22 at 01:52

1 Answers1

0

There is nothing wrong with that code, the only way you can "simplify" that code, better to say "abstract it" is by creating a controller with a method that returns the view.

In your case, if your route is very specific you can create a single action controller with the command:

php artisan make:controller PostController -i.

Then in the controller:

public function __invoke(Request $request)
{
    return view("post1");
}

And in your routes file:

Route::post('/post1', PostController::class);

More info in the Single Action Controller docs and in the views docs

Guille
  • 614
  • 3
  • 10