In Laravel, you can define a route with a controller action by following these steps:
Step 1: Define a route in the routes/web.php file or in the routes/api.php file.
For example, in the web.php file:
Route::get('/example', 'ExampleController@index');
Step 2: Create a controller using the Artisan command:
php artisan make:controller ExampleController
This command will create a new file named ExampleController.php in the app/Http/Controllers directory.
Step 3: Open the ExampleController.php file and define an index method (or any other desired method) that will be associated with the route:
namespace App\Http\Controllers;
class ExampleController extends Controller
{
public function index()
{
// Your code logic here
}
}
Step 4: Add the logic you want to execute in the index method or any other method, according to your requirements.
Now, when you access the '/example' route, Laravel will execute the index method of the ExampleController.