I am new to the Laravel framework and I am finding that on many of my views, I need to have similar code as seen below. I am sure this is not best practice. Can someone advise me what would be the best practice approach on this?
<?php
use App\Song;
use App\Music;
$songArtist = Song::find('5b0a6a779a892025b869f552')->Artists()->get();
?>
<div class="form-group row">
...
</div>
I could then use the $songArtist variable... Note, I am using MongoDB on the backend.
You should read on the MVC or Model-View-Controller architecture. There are many arguments as to whether Laravel is an MVC Framework or not, but in this case, let's assume that it uses the said architecture.
The way you pull the data/logic is normally handled by the model. The controller will be handling the way the data will be transported into the view (html). Lastly, the view will serve as the user-interface. This should make your code cleaner and organized.
A typical example would be like this:
Controller
use App\Song;
use App\Music;
function getArtist()
{
$songArtist = Song::find('5b0a6a779a892025b869f552')->Artists()->get();
return view('myhtml')->with('songArtist', $songArtist);
}
View
<div class="form-group row">
<p> {{ $songArtist}} </p>
</div>
Related
Let's say that I have the following main view:
<div>
<div>
#yield('content')
</div>
<div>
#include('sidebar')
</div>
<div>
#include('footer')
</div>
</div>
How would I make sure that the included views have their own data? For example, in the sidebar I want to display, let's say, new blog post comments. In the footer I want to display new users and blog posts. Maybe using #include is not the right way to accomplish this?
In your applications, app service providers boot method do something like
View::composer('sidebar',function($view){
$comments= Blog::latest()->comments;
$view->with('comments',$comments);
})
View::composer('footer',function($view){
$users= User::latest();
$view->with('users',$users);
})
//sidebar
Then in the view file do something like
#foreach($comments as $comment)
{{$comment->data}}
#endforeach`
You may want to use View Composers in such case.
Write your logic in View Composer then call it using a service provider, in the boot method
View::composer(
['partials.content', 'partials.sidebar', 'partials.footer], 'App\Http\ViewComposers\MyViewComposer'
);
I'm tried this and it's working with me :
#include('content',array("key"=>"value"))
I am looking for a very general way to include bootstrap components in my blade view. For example let's say I need a drop down in my view, should I make a partial called dropdown.blade.php with code as follows:
<div class="dropdown">
<a href="#" class="dropdown-toggle" type="button" data-toggle="dropdown">
<span class="glyphicon glyphicon-chevron-down"></span></a>
<ul class="dropdown-menu">
#foreach ($options as $option)
<li>{{$option["name"]}}</li>
#endforeach
</ul>
</div>
and use it in my view in the following way:
#include('partials.dropdown',
array("options"=>array(
["href"=>"#", "name"=>"Profile"],
["href"=>"#", "name"=>"Report"],
)))
Even we can make it more generic by adding options for button name etc. Is it a good or preferable way to do it or should we use copy-paste method from bootstrap website to our views every time? Is there any package that is doing this sort of work? Can we make it in more elegant way?
This seems like a good idea if you are going to re-use the component a lot. I think the more elegant way to do it would be to create custom blade directives:
https://laravel.com/docs/master/blade#extending-blade
Then you could do, for instance:
#dropdown($options, 'btn-primary')
I would also provide an argument for a custom element ID or name, so you can reference it elsewhere on the page as needed.
This gets a little more complex with things like modals. I think you'd want to register multiple blade directives so you could do something like
#startmodal
#modaltitle('Title')
#startmodalbody
Some body content
#endmodalbody
#endmodal
I have to create multi page angular framework using ng boilerplate. We have modular component based approach and single component can be created multiple times on same page. For example I can have 2 instance of carousel component on home page and there configuration and slides parameter for image path etc are coming from ajax. Now challenge is that this ajax url is dynamic and there is no fixed pattern so I cant hard code in my js. is there any way I can pass this dynamic url from template to my $http request?
Something like this in
<div ng-controller="CarouselCtrl" carouselUrl="<dynamic url>">
<div class="container slider">
<ul>
<li ng-repeat="slide in slides">//..</li>
</ul>
</div>
</div>
You can pass attributes to controllers only in directives. Moreover, you might rethink having your CarouselCtrl logic in separate directive, as this is clearly the case where this should be done.
Considering this example markup (from http://www.ng-newsletter.com/posts/validations.html):
<div class="row">
<div class="large-12 columns">
<label>Your name</label>
<input type="text"
placeholder="Name"
name="inputNameAttributeValue"
ng-model="signup.name"
ng-minlength=3
ng-maxlength=20 required />
<div class="error"
ng-show="signup_form.name.$dirty && signup_form.name.$invalid">
<small class="error"
ng-show="signup_form.name.$error.required">
Your name is required.
</small>
<small class="error"
ng-show="signup_form.name.$error.minlength">
Your name is required to be at least 3 characters
</small>
<small class="error"
ng-show="signup_form.name.$error.maxlength">
Your name cannot be longer than 20 characters
</small>
</div>
</div>
</div>
Is there a way to accomplish the same thing, but use JavaScript instead of custom Angular attributes?
For example, is there a way I can use JavaScript instead of these Angular html attributes: ng-model, ng-minlength, ng-maxlenth, ng-show?
EDIT:
Just to clarify, I'm looking for a solution that uses the Angular JavaScript API. I would like to have a separate JavaScript document (linked from my HTML document) that uses the Angular JavaScript API. For example, is there a way to specify ng-model for a particular form field using the Angular API instead of the custom Angular HTML attribute?
As I understand it, you want to add a directive (for example ng-model) from javascript, similar to how you would do it with jQuery. Short answer: Don't do it.
Longer Answer: It's probably technically possible, but it would violate the basic principles of AngularJS. Your controller should not touch the HTML at all, in fact any code which should directly manipulate the HTML should be placed in a directive. And that directive should be placed on the input directly in your HTML, which is exactly what you wanted to avoid.
If placing directives in your HTML is not practical for your project, then perhaps you should reconsider using AngularJS.
There's a rather long (and well written) answer here on Stackoverflow which explains "how to think in AngularJS", you might find that it's of interest: https://stackoverflow.com/a/15012542/179024
It would also be interesting to know why you want to do this? There is often an "Angular way" of doing things, but it can be different from what we are used to doing.
pretty much ripping my hair out over how badly designed the decorators are in Zend Framework.
I've spent a lot of today and a few other days trying to figure out how to do something that should be a simple frontend task.
Zend_Forms and decorators in my opinion are the worst part of ZF as backend and frontend are not properly seperated. Why is the form class dictating how the HTML should be printed?
All I'm looking for, is something simple like:
<div class="labeledField">
<label for="username">Username</label>
<input type="text" id="username" name="username" />
</div>
<div class="labeledField">
<label for="password">Password</label>
<input type="password" id="password" name="password" />
</div>
The reason I want it like this is because I want the label to sit on top of the field like:
[Username ]
[Password ]
This way I can make the label slightly fade when the input is selected but still remain when the input is selected. Using JS the label is hidden when the input box contains anything. This functionality exists on the Apple shopping cart.
I love the validation parts of Zend_form and I would love how it can be placed in the frontend if backend code wasn't dictating how the HTML looks.
In my opinion it should either take a template (from a frontender with access to the /views/ folder or I should be able to do something like:
<div class="labeledField">
<?php
echo $this->form->username->getLabel();
echo $this->form->username->getElement();
$errors = $this->form->username->getErrors();
if (sizeof($errors) > 0) {
?>
<div class="errors">
<?php
foreach($errors as $error) {
?>
<li><?php echo $error ?></li>
<?php
}
?>
</div>
<?php
}
?>
</div>
But then the templating would allow me to just echo $this->form and have it use the format as above for fields I want to.
No question about it, decorators can take some getting used to. Let me address/answer some of your issues/questions:
pretty much ripping my hair out over how badly designed the decorators are in Zend Framework. I've spent a lot of today and a few other days trying to figure out how to do something that should be a simple frontend task.
Yep, it's a miracle I have any hair left at all.
Zend_Forms and decorators in my opinion are the worst part of ZF as backend and frontend are not properly separated. Why is the form class dictating how the HTML should be printed?
I agree with you here that how the form is to be rendered is a presentation issue, so it's probably more view-related. If you really wanted to separate the concerns, you could create your forms undecorated and then add decorators in your view-script, perhaps using some custom view-helpers. This makes the most sense to me, though I confess I have never bothered to do it.
For examples of a standalone class that sets decorators on a separate form, check out EasyBib_Form_Decorator and Using Zend_Form without Zend Framework MVC
In my opinion it should either take a template (from a frontender with access to the /views/ folder or I should be able to do something like:
You pretty much do have that functionality using the ViewScript decorator.
$form->setDecorators(array(
array('ViewScript', array( // note case
'viewScript' => '_partials/forms/my.phtml', // note case
)));
That said, your desired markup is relatively straightforward using standard decorators:
// not tested, but should be pretty close
$element->setDecorators(array(
'ViewHelper',
'Label',
'Errors',
array('HtmlTag', array('tag' => 'div', 'attribs' => array('class' => 'labeledField'))),
));
Of course, any special styling or client-side manipulation of the form - for example, the Apple cart effect you cite - can be layered on to this markup on the client-side.