1

I have some header code that I don't want in the controller and I just want it in the view, but because of php tags around the code I don't know how would I work this within the blade,

I have something like this

links.blade.php

 $links = array(
    'tab1'=>array('_'=>'a', 'href'=>'link1', 'link1'),
    'tab2'=>array('_'=>'a', 'href'=>'link2', 'link2'),
    'tab3'=>array('_'=>'a', 'href'=>'link3', 'link3'),
    'tab4'=>array('_'=>'a', 'href'=>'link4', 'link4'),
    );
?>

header.blade.php

<?php
switch () {
//other header code

@include('links')  //id like to include it here

//other header code
}

?>

how would I include links.blade.php array or a variable into header.blade.php ? Or do I have to create the controller in order to do this ?

4
  • 1
    I hope this doesn't come off as sounding rude but if you aren't writing controllers and you are trying to add this kind of logic to the views, Laravel isn't the right tool for this job. It's just going to keep getting in your way. Commented Apr 27, 2016 at 20:09
  • @user3158900 even though it is header stuff I should actually put it in the controller ? Commented Apr 27, 2016 at 20:14
  • 1
    By header stuff, if you mean it's something which will go into every page, the best place would be to put it into a view composer so that whenever header.blade.php is loaded, it automatically receives this variable. Commented Apr 27, 2016 at 20:31
  • thanks for suggestion, I will take a look at view composer, I appreciate it Commented Apr 27, 2016 at 20:41

1 Answer 1

1

Try this:

<?php
switch () {
//other header code
?>

@include('links')  //id like to include it here

<php?
//other header code
}
?>

Or this (will work as switch):

@if ()
@include('links')
@elseif ()
@endif
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, would the yield and content work as well , if I have a similar situation ?
If you want to insert a file, it's better to use @include. Also, @user3158900 is right and you should use DB if you're going to keep links somewhere. If you have just few links and you're sure that you will not add any further, I think it's ok to keep them in a file.
I will definitely use controllers but in this case I will not use them , it's just few links

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.