#4 Controllers in Laravel
Why Laravel Controllers
Controller is one of the major part of MVC (Model-View-Controller) architecture. In our previous lecture, we've learned about laravel routing, now it might be not good to write all the business logic in a one file in route - web.php.
To write logic what a route will do, we'll write that in controller. We'll understand the concept of Laravel Controllers now. Default Controller location of Laravel is
app/Http/Controllers
Basic Controller Example in Laravel
Here is a basic controller example in Laravel with Laravel Route -
use App\Http\Controllers\PostsController;
Route::get('posts', [PostsController::class, 'index'])->name('posts.index');
So, in the PostsController, we've to make a function called index, and the format would be like this in Laravel Controller.
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Post;
class PostsController extends Controller
{
/**
* Show All Post Resource
*
* @return View
*/
public function index()
{
$posts = Post::all();
return view('posts.index', compact('posts'));
}
}
So, In controller here is some key points -
- Laravel Controller Extend or inherit the Laravel's Base/Parent Controller class.
- We've to import models or other classes at top, like
use App\Models\Post
for Post Model's import system. - We've to define a
public function functionName(){ // }
to make a function that is visible to all. - Inside function, we can fetch data like $posts = Post::all() to find all posts
- We then finally return a view and pass that data to the view. Like, in the above example - We've created posts variable and we pass this data to the
posts.index
view. This view file is inside theposts/index.blade.php
. - To pass data to the view, we could use
compact()
function, or just array [].
Pass Parameter in URL and Pass to Controller Laravel
Assume, we want to create an url like this with post slug, http://test.com/posts/test-post
Where test-post is the post's slug. So, our Route and Controller code would be -
use App\Http\Controllers\PostsController;
Route::get('posts/{slug}', [PostsController::class, 'show'])->name('posts.show');
Official Documentation of Laravel - https://laravel.com/docs/7.x/controllers
Laravel 8 Contrller's documentation - https://laravel.com/docs/8.x/controllers
All Tutorials in this playlist
Popular Tutorials
Categories
-
Artificial Intelligence (AI)
11
-
Bash Scripting
1
-
Bootstrap CSS
0
-
C Programming
14
-
C#
0
-
ChatGPT
1
-
Code Editor
2
-
Computer Engineering
3
-
CSS
28
-
Data Structure and Algorithm
18
-
Design Pattern in PHP
2
-
Design Patterns - Clean Code
1
-
E-Book
1
-
Git Commands
1
-
HTML
19
-
Interview Prepration
2
-
Java Programming
0
-
JavaScript
12
-
Laravel PHP Framework
37
-
Mysql
1
-
Node JS
1
-
Online Business
0
-
PHP
28
-
Programming
8
-
Python
12
-
React Js
19
-
React Native
1
-
Redux
2
-
Rust Programming
15
-
SEO - Search Engine Optimization
1
-
Tailwind CSS
1
-
Typescript
10
-
Uncategorized
0
-
Vue JS
1
-
Windows Operating system
1
-
Woocommerce
1
-
WordPress Development
2
Tags
- Artificial Intelligence (AI)
- Bash Scripting
- Business
- C
- C Programming
- C-sharp programming
- C++
- Code Editor
- Computer Engineering
- CSS
- Data Structure and Algorithm
- Database
- Design pattern
- Express JS
- git
- Git Commands
- github
- HTML
- Java
- JavaScript
- Laravel
- Mathematics
- MongoDB
- Mysql
- Node JS
- PHP
- Programming
- Python
- React Js
- Redux
- Rust Programming Language
- SEO
- TypeScript
- Vue JS
- Windows terminal
- Woocommerce
- WordPress
- WordPress Plugin Development