How to create new page in laravel
In Laravel, you can create a new page by creating a new blade template and a new controller method. Here's a step-by-step guide:
Step 1: Create a new blade template
- Open the terminal and navigate to the
resources/views
directory. - Run the command
php artisan make:view <page-name>
(e.g.,php artisan make:view about
). - This will create a new blade template file in the
resources/views
directory with the name you specified.
Step 2: Create a new controller method
- Open the terminal and navigate to the
app/Http/Controllers
directory. - Run the command
php artisan make:controller <controller-name>
(e.g.,php artisan make:controller AboutController
). - This will create a new controller file in the
app/Http/Controllers
directory with the name you specified.
Step 3: Define the controller method
- Open the newly created controller file and add a new method with the same name as the page you want to create (e.g.,
about
). - In this method, you can use the
View
facade to render the blade template you created in Step 1.
Here's an example:
// app/Http/Controllers/AboutController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;
class AboutController extends Controller
{
public function about()
{
return View('about');
}
}
Step 4: Define the route
- Open the
routes/web.php
file and add a new route that points to the controller method you created in Step 3.
Here's an example:
// routes/web.php
Route::get('/about', 'AboutController@about');
Step 5: Test the page
- Open your web browser and navigate to the URL you defined in the route (e.g.,
http://localhost:8000/about
). - You should see the new page rendered with the blade template you created.
That's it! You have now created a new page in Laravel.
Note: Make sure to update the composer.json
file and run composer dump-autoload
to register the new controller and route.