loading...
Cover image for Laravel Tutorial #6: Routes and Controllers

Laravel Tutorial #6: Routes and Controllers

ericnanhu profile image Eric Hu Updated on ・3 min read

As I introduced in the previous sections of this tutorial, a simple blog should have a home page, a category page, a tag page and a single post page. Each page has a corresponding route and a controller.

Routes

First, we need one route for each controller. We already talked about routes in the previous sections. Go to routes/web.php.

Route::get('/', 'HomeController@index');
Route::get('category/{slug}', 'CategoryController@index');
Route::get('tag/{slug}', 'TagController@index');
Route::get('post/{slug}', 'PostController@index');

slug is the variable that is passed to the index() method of CategoryController

Index Controller

First, create an IndexController using the artisan command tool:

php artisan make:controller IndexController

Open app/Http/Controllers/IndexController.php.

Second, include the necessary models. We need data about categories, tags, posts and users for our Home page.

use App\Post;
use App\Category;
use App\Tag;
use TCG\Voyager\Models\User; //If you are using Voyager

Finally, we can create our index() function:

public function index()
{
    //get the posts that are published, sort by decreasing order of "id".
    $posts = Post::where('is_published',true)->orderBy('id','desc')->get();

    //get the featured posts
    $featured_posts = Post::where('is_published',true)->where('is_featured',true)->orderBy('id','desc')->take(5)->get();

    //get all the categories
    $categories = Category::all();

    //get all the tags
    $tags = Tag::all();

    //get the recent 5 posts
    $recent_posts = Post::where('is_published',true)->orderBy('created_at','desc')->take(5)->get();

    //return the data to the corresponding view
    return view('home', array(
        'posts' => $posts,
        'featured_posts' => $featured_posts,
        'categories' => $categories,
        'tags' => $tags,
        'recent_posts' => $recent_posts
    ));
}

We haven’t created the views yet, I’ll talk about them in the next post.

Category Controller

php artisan make:controller CategoryController

Include models:

use App\Post;
use App\Category;
use App\Tag;
use TCG\Voyager\Models\User; //If you are using Voyager

index() function:

public function index($slug)
{
    //get the requested category
    $category = Category::query()->where('slug', $slug)->first();

    //get the posts in that category
    $posts = $category->posts()->get();

    //get all the categories
    $categories = Category::all();

    //get all the tags
    $tags = Tag::all();

    //get the recent 5 posts
    $recent_posts = Post::where('is_published',true)->orderBy('created_at','desc')->take(5)->get();

    //return the data to the corresponding view
    return view('category', array(
        'category' => $category,
        'posts' => $posts,
        'categories' => $categories,
        'tags' => $tags,
        'recent_posts' => $recent_posts
    ));
}

Tag Controller

php artisan make:controller TagController

Include models:

use App\Post;
use App\Category;
use App\Tag;
use TCG\Voyager\Models\User; //If you are using Voyager

index() function:

public function index($slug)
{
    //get the requested tag
    $tag = Tag::query()->where('slug', $slug)->first();

    //get the posts with that tag
    $posts = $tag->posts()->get();

    //get all the categories
    $categories = Category::all();

    //get all the tags
    $tags = Tag::all();

    //get the recent 5 posts
    $recent_posts = Post::where('is_published',true)->orderBy('created_at','desc')->take(5)->get();

    //return the data to the corresponding view
    return view('tag', array(
        'tag' => $tag,
        'posts' => $posts,
        'categories' => $categories,
        'tags' => $tags,
        'recent_posts' => $recent_posts
    ));
}

Post Controller

php artisan make:controller PostController

Include models:

use App\Post;
use App\Category;
use App\Tag;
use TCG\Voyager\Models\User; //If you are using Voyager

index() function:

public function index($slug)
{
    //get the requested post, if it is published
    $post = Post::query()->where('is_published', true)->where('slug', $slug)->first();

    //get all the categories
    $categories = Category::all();

    //get all the tags
    $tags = Tag::all();

    //get the recent 5 posts
    $recent_posts = Post::where('is_published',true)->orderBy('created_at','desc')->take(5)->get();

    //return the data to the corresponding view
    return view('post', array(
        'post' => $post,
        'categories' => $categories,
        'tags' => $tags,
        'recent_posts' => $recent_posts
    ));
}

Next Post: Laravel Tutorial #7: Views

Related Articles

How to Make Your Server More Secure

Laravel Tutorial For Beginners

Django Tutorial For Beginners

Posted on by:

Discussion

pic
Editor guide