Skip to content
This repository has been archived by the owner on Sep 17, 2018. It is now read-only.

Laravel and Bard

Mario Bašić edited this page Jul 4, 2017 · 8 revisions

Dependency injection in Laravel

If you are using Laravel I suggest using dependency injection when dealing with Bard.

Below you can see an example on how to use DI and create a sitemap by adding URLs.

<?php 

namespace App\Http\Controllers;

use Laravelista\Bard\UrlSet as Sitemap;
use LaravelLocalization;

class SitemapController extends Controller {

    public function generate(Sitemap $sitemap)
    {
        $sitemap->addUrl(route('home'))
            ->setPriority(1.0)
            ->setChangeFrequency('always')
            ->addTranslation('hr', $this->getLocalizedURL('hr', route('home')));

        $sitemap->addUrl(route('contact'))
            ->setPriority(0.8)
            ->setChangeFrequency('never')
            ->addTranslation('hr', $this->getLocalizedURL('hr', route('contact')));

        $sitemap->addUrl(route('about'))
            ->setPriority(0.5)
            ->setChangeFrequency('yearly')
            ->addTranslation('hr', $this->getLocalizedURL('hr', route('about')));

        return $sitemap->render();
    }

    /**
     * Use LaravelLocalization to get localized URL for given URL.
     *
     * @param $locale
     * @param $url
     * @return string
     */
    private function getLocalizedURL($locale, $url)
    {
        return LaravelLocalization::getLocalizedURL($locale, $url);
    }

}
Clone this wiki locally