Laravel - Use function in blade - php

I'm beginning to build an app using Laravel, and one of the challenges I'm now facing is that throughout my blade views, i need to show a bunch of different bootstrap modals. Because of this, i believe it would be a better approach to create some sort of helper method that takes a few parameters to define how the modal should look. But im currently not sure what is best practice of doing this. Am i even allowed to use such a helper method in a view?
How would you do this?

You can still process any data using the PHP tags (<?php echo 'hi'; ?>), or even using the complete namespace of the file and then call the desired function, like {{ \Carbon\Carbon::today() }}

You can create a helper file and then define methods which can be used globally, including the views. But in your case the ideal scenario would be to use a partial and then to include the partial with different parameters to change the properties. This would work exactly like a method, but cleaner and more aligned with views. This should help you, the sub-views are called partials.
https://laravel.com/docs/5.4/blade#including-sub-views

Related

Laravel: Will Helper functions work for it?

I am using Multiple code blocks of Mail:: and Gate::define() in my code. I rather want to make it as functions and call them. What approach would be useful? Should I create helper classes?
If You have some logic and you want to use this logic from many places, you must to encapsulate it, an in your situation it's not bad solution to create helper function or maybe helper class.
If You don't know how - here is very clear example to do it :
Creating a Helpers File

How to integrate menu/footer/menu in PHP MVC

i know that this question already has been asked. But i still dont understand it.
Right now im trying to understand how to make use of the MVC Modell.
But i dont get how to integrate everything and right now I am hesitating to use a framework before i really get it.
What I try to achieve:
I want to have a header, a footer and a menu which remain the same all the time.
Like this:
HEADER
MENU
{CONTENT}
FOOTER
So my thinking is:
My Controller gets some Information, lets say its a User-ID.
Controller calls a method from the Model:
Get all DATA from USER with ID: 1
Then the controller passes the DATA into a view, lets say a list.
So where to go from here?
Should the controller pass this view into another view?
Like:
$site = new View(); <br>
$site->wholeSite($content);
and then site is something like:
HEADER
MENU
{$content}
FOOTER
Please excuse my simple approach, im just trying to get the basic idea behind it and i already read the first 20 pages of googling it.
Just cant get my head around it.....
It would be really nice if you would explain it for an Beginner :)
Thanks in advance!
you can call multiple views from the control like:
$this->load->view('header',$data);
$this->load->view('menu',$data);
$this->load->view('content',$data);
$this->load->view('footer',$data);
If you have a nested div then you can use regular include function like from a particular view file.
include('footer.php');
According to the path of the view file. The data also gets transferred from parent view to the included file.
Generally MVC apps have a universal layout view which contains the header, footer and menus. Sometimes data required for that is handled outside the controller, in say your init script or bootstrap.
An MVC purist might pass all data the layout requires from the controller, but that can be repetitive, although setting the data in an abstract controller construct might alleviate that.
On the question of views and sub-views there are generally two solutions:
A.) You have one principle view which all required data is passed to. In that view sub-views (partials) are called, and data is passed down into them.
B.) You instantiate all your views in the controller, passing all the data they need directly into the view, you would then pass views as parameters into other views.
Both methods have their pros and cons. I find the first option leads to less Fat Controllers, so I usually go with that.
I'd recommend you learn an existing MVC framework ( CodeIgniter, now defunct is basic enough to make it a good entry point), as that will show you how things are done.

How do I place a View belonging to a Plugin in regular View (CakePHP Howto)

I would like to get some input regarding the feasibility of the following scenario/design pattern:
I would like to insert a View from a Category Plugin (/category/categories/tree) into one of my application Views (/posts/edit/3) as if it is an CakePHP element.
The inserted View has a high level of functionality (sorting, adding, deleting, etc...) and therefore simply calling echo $this->element(); does not seem appropriate to me. That 'feels' more like a practice appropriate in situations where 'snippet'/lower level functionality is needed.
My question: is this possible in CakePHP, if so: how to do this? (just a rough outline of the way to go would suffice)
My first idea is to call an element from the plugin and use $this->requestAction(); from the element.
But as I said earlier is my association with elements one of little functionality/snippet. Using a controller method and its View 'feels' more appropriate. But I don't know how to 'call' a View within a View.
Main reason why I would want this:
full Controller functionality.
activate helpers based on Plugin requirements (this should be of no concern to the posts-controller)
beforeFilter and beforeRender of Plugin might be of use. For example: $this->set('modelName', $this->modelClass);
You could look at using view blocks. AFAIK, they are generally designed to help solve more complex view construction, like the problem you are considering here.
Or call a plugin element directly.

Where to put big, rarely used functions in codeigniter

I have a several functions which are quite large, and are only used in one controller function each, and I'm wondering where to put these? They are not displaying any views, but instead crunching some numbers.
If I'm not wrong, there are 4 possible places where i could put these function: in my controller, in a helper, in a library or in a model. But none of these seem appropriate, since I don't want the code to be loaded every time a user uses the controller, and model should be used to do database stuff, and helpers and libraries should contain code that can be used over and over again.
If it is business logic, the best place to put it is in the controller as a private method, then you can call that method from within the controller.
Just as a note, helpers aren't always loaded unless you autoload them or load them in the constructor of your controller. So, as an alternative, you can make these methods of a helper then just load the helper in the controller action you wish to use them. That way they only get loaded when you need them.
CodeIgniter comes with helpers that you probably might not use (doesn't load unless you specify it in the application/config/config.php file) and I don't think its a problem having functions that you only use once stored there (application/helpers ). For example I might use a random password generator once only, but its still there and won't be loaded unless I call it.
$this->load->helper('my_string_generators');

Coding standards - Controller vs view

I am writing this web project in which one of the views will be having two types of boxes. There is an array and based on type of the values of each item in the array i have to display one or the other box.
Qn is: Should i write the html code for boxes in the controller as two methods or should i write it as two functions and call on it from inside the view itself? Both methods sounds not so good. Your thoughts,suggestions or any ideas for a method outside of two i mentioned above?
Here is a link i saw which closely resembles my thought on writing html code in controller. PHP coding standards
MVC is a guideline. It's there to help you, if it's hindering you getting the job done then something is wrong.
I'm not sure I understand your question properly, but if you mean that you have to display different HTML depending on the data passed to you during run time, then I'd suggest you package that code in a helper function and call it from your view.
On the other hand, if you mean that you're views are well defined and unchanging, I'd just do the checking in the controller and display the appropriate view.
You should follow the common way of dealing with view using MVC guidelines, meaning you should populate a variable with your two functions inside the controller with the values - then inside your view, you should fetch the raw data and display it as you choose.
I'm not sure I understood you completely, but :
No HTML code in your controller.
If you have 2 different HTML code,
and one controller action, then you
need 2 views. In your controller, you
can then choose which view to use.

Categories