Basic routing, controllers, views, and models in Laravel (or another framework)
So, let’s take a closer look at the fundamental features of routing, controllers, views, and models in Laravel, one of the most popular PHP frameworks. All of these concepts and methods are fundamental to any MVC (Model-View-Controller) development framework, and the Laravel PHP framework makes it easy for developers to implement them.

Routing in PHP Framework.
The routing concept in the Laravel PHP framework is a process by which the PHP framework helps map HTTP requests, such as navigating a webpage or calling an API, to specific actions in the user application.
Defining Routes in PHP Framework.
In the Laravel PHP framework, routes for the web-based route process concept are defined in the routes/web.php PHP framework file, and for API routes, in the routes/api.php file.
Laravel PHP Framework Example.
// routes/web.php file
Route::get(‘/’, function () {
return view(‘welcome to laravel php framework’);
});
// Let’s create a basic route to display any text message
Route::get(‘/welcome’, function () {
return ‘hi, welcome to laravel framework’;
});
// Let’s create a Route with actual parameters
Route::get(‘/employee/{id}’, function ($id) {
return “employee ID: ” . $id;
});
// Let’s create a Route using a controller
Route::get(‘/about’, ‘PageController@about’);
Laravel PHP Framework Example explanation.
- GET – This example uses the HTTP method to retrieve data from a dedicated host web server.
- Here, web developers can also implement other HTTP methods like POST, PUT, DELETE, etc. in the Laravel PHP framework.
- In this example, the {id} in the route is a route parameter argument that is passed to the controller method.
Controller in PHP Framework.
Controllers in the Laravel PHP framework are responsible for managing and controlling the logic or arguments behind routes. They receive requests to the Laravel PHP framework, process user data, and return a system response, usually a view value.
Creating a Controller in the Laravel PHP Framework.
Developers can create a controller in the Laravel PHP framework using the command-line tool Artisan.
php artisan make:controller PageController
This command creates a controller file in app/Http/Controllers/PageController.php.
Defining controller methods in the Laravel PHP framework.
Here you find an example controller in the Laravel PHP framework, with a view return method defined.
// app/Http/Controllers/PageController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PageController extends Controller
{
// Create an About page controller
public function about()
{
return view(‘about’); // It displays or returns the “about” view
}
}
In this, when the user navigates to the /about route, the about() method in PageController returns a view value named about.
Linking controllers and routes in the Laravel PHP framework.
Here, developers can connect routes to controller methods as needed by linking them like this:
// routes/web.php
Route::get(‘/about’, ‘PageController@about’);
When a user navigates to the /about page in their web browser, the about() method in the PageController will be activated.
Views in the Laravel PHP Framework.
Views in the Laravel PHP Framework are used to separate an application’s logic from its presentation. They contain HTML source code and can be modified as needed with Blade, the Laravel PHP framework’s templating engine.
Creating Views in the Laravel PHP Framework.
Views in the Laravel PHP Framework are typically stored in the resources/views directory.
Here, we create a file for the about.blade.php view in resources/views.
<!– resources/views/about.blade.php –>
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<title>About Us Page</title>
</head>
<body>
<h1>Content of About Page</h1>
<p> About page content about the website.</p>
</body>
</html>
The blade.php file extension in the Laravel PHP framework indicates that this file is a Blade template file, allowing web developers to use Blade syntax such as {{ $variable }} for dynamic content.
Returning a view from a controller in the Laravel PHP framework.
In a controller method in the Laravel PHP framework.
public function about()
{
return view(‘about page’);
}
This commands the Laravel PHP framework to load the about.blade.php view file.
Models in PHP Framework.
Models in the Laravel PHP framework represent the application data layer. These systems directly interact or communicate with the backend database, and provide web developers with features to query and manipulate user data.
Creating Models in the Laravel PHP Framework.
Web developers can create a model using Artisan like this:
php artisan make:model Post
This statement here creates a Post model in app/Models/Post.php.
Defining Models in the Laravel PHP Framework.
A Post model is typically a table in the user database; for example, it represents a Post table. Here’s an example of a basic model.
// app/Models/Post.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
// Here this table is associated with the extended model
protected $table = ‘posts’;
// Here we use mass assignable table model attributes
protected $fillable = [‘title’, ‘content’];
}
The $table property here is an optional option and is only necessary if the user table name does not follow Laravel’s plural and snake_case naming rules.
The $fillable property indicates which attributes can be assigned at the mass level. This is used when creating or updating records.
Interacting with the Database.
Users can interact with the Post model in a Laravel controller to interact with records from the database.
// app/Http/Controllers/PostController.php
namespace App\Http\Controllers;
use App\Models\Post;
class PostController extends Controller
{
public function index()
{
$posts = Post::all(); // Here it is used to retrieve all posts
return view(‘posts.index’, compact(‘posts’)); // Here it is passed to the view
}
}
Displaying data in a Laravel PHP framework view.
In a Laravel view (resources/views/posts/index.blade.php), developers can loop through and display a user post.
<!– resources/views/posts/index.blade.php –>
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<title>All website Posts</title>
</head>
<body>
<h1>display All website Posts</h1>
<ul>
@foreach ($posts as $post)
<li>{{ $post->title }}</li>
@endforeach
</ul>
</body>
</html>
In this example, the index() method in the PostsController retrieves all website posts from the database and passes them to a Laravel view method. The view then loops through the posts and displays the title of each post.
Complete flow example of a basic Laravel app.
Let’s assume a user wants to create a simple blog for their website, displaying all user-created posts from multiple websites.
So, let’s define a route.
Route::get(‘/posts’, ‘PostController@index’);
Create a controller.
php artisan make:controller PostController
Here, define the index() method in PostController to receive data and send it to the view.
public function index()
{
$posts = Post::all();
return view(‘posts.index’, compact(‘posts’));
}
Create a Laravel view (resources/views/posts/index.blade.php).
<h1>Website Blog Posts</h1>
<ul>
@foreach ($posts as $post)
<li>{{ $post->title }}</li>
@endforeach
</ul>
Define the Laravel model (Post.php).
class Post extends Model
{
protected $fillable = [‘title’, ‘content’];
}
Migrate the website database (if necessary) – If the website user or developer doesn’t already have a migration file for the posts table, create one.
php artisan make:migration create_posts_table
Define the schema in the website database migration file and run the migration file.
php artisan migrate
Conclusion of Basic routing, controllers, views, and models in Laravel.
- In this example, you’ve learned how routing, controllers, views, and models work together in the Laravel PHP framework.
- In the Laravel PHP framework, routes define the URLs to which your app provides responses.
- Controllers manage the logic for processing requests and sending data to views.
- Views display the user interface and can represent dynamic data sent from controllers.
- Models interact with the database, allowing users to store, retrieve, and manage data.

