Junior — Middle
What is the design pattern used in Laravel for organizing code?
sobes.tech AI
Answer from AI
The MVC (Model-View-Controller) design pattern is widely used in Laravel. It divides the code into three main parts:
- Model — responsible for data handling and business logic.
- View — responsible for displaying data to the user.
- Controller — processes requests, interacts with the model, and selects the view.
This separation simplifies maintenance and development of the application, improves readability, and promotes code reuse.
Example of a controller in Laravel:
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function index()
{
$posts = Post::all();
return view('posts.index', compact('posts'));
}
}