Search code examples
laravellaravel-routing

Laravel display SEO tag automatically through Route name


I have question about Laravel.

I want display SEO tag automatically from Database but I do not know how to do.

I have route like this

Route::get('/', [
    'uses' => 'SeoController@index',
    'as' => 'homepage'
]);
Route::get('/about', [
    'uses' => 'SeoController@index',
    'as' => 'about'
]);

From SeoController I want to display view base on Route url;

public function index()
    {
        switch ($route) {
            case '/':
                $title = "Homepage";
                return view('welcome', ['title'=> $title]); 
                break;
            case '/about':
                $title = "About page";
                return view('about', ['title'=> $title]);
                break;
            default:
                break;
        }
    }

How can I check $route to know which route come?

Thank you so much


Solution

  • I would love to suggest a better way of doing this in Laravel.

    In Laravel, you would want to define different controller methods for each pages and return a view like so:

    class SeoController extends Controller
    {
        public function home()
        {
            return view('home');
        }
    
         public function about()
        {
            return view('about');
        }
    
        public function contact()
        {
            return view('contact');
        }
    }
    

    Ensure you have the routes registered in web.php as:

    Route::get('/', [
        'uses' => 'SeoController@home',
        'as' => 'homepage'
    ]);
    
    Route::get('/about', [
        'uses' => 'SeoController@about',
        'as' => 'about'
    ]);
    
    Route::get('/contact', [
        'uses' => 'SeoController@contact',
        'as' => 'contact'
    ]);
    

    And also ensure you have the corresponding blade file for each of these views in the view folder.