Support the ongoing development of Laravel.io →
posted 4 years ago
Last updated 1 year ago.
0

Hi ARESLAX,

please try the following

class PagesController extends Controller
{
    protected $links;

    public function __construct()
    {
        
        $this->links = [
	   ['Home','/'],
	   ['About','/about'],
	   ['Contact','/contact']
        ];
    }
    public function home()
    {
        return view('welcome','links'=>$this->links);
    }
    public function about()
    {
        return view('about',['links'=>[$this->links]]);
    }
}

you could also stick this in the controller.php file so it will be available in all your controllers

I would also suggest having a read of the following https://laravel.com/docs/5.8/views#view-composers as I think this might be a nicer option for something like links as they are injected into every page without having to specify them in every controller

0

Thank you! That works as intended.

I'm just getting up to speed with PHP classes and Laravel, and I will read the link you suggested.

I appreciate your attention and solution.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

ARESLAX areslax Joined 23 Aug 2019

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.