In the header of my site I have to add some meta-tags that are stored in the database. I have created a Metatag model and made it auto-loadable in autoload.php.
This model will provide all the meta-tags and they will be inserted in the header of all views, of any route. Can the model's meta-tags array (retrieved from the table) be somehow directly and globally used in the view or do I have to create it in every controller then pass it to the view through $data?
I have quite a few controllers and I would like to avoid requesting the meta-tags in each of them, specially when the model is auto-loaded globally.
I do not think there was any other way than to go through the controller . If that were the case, the MVC pattern would no longer respect what goes against the rule CodeIgniter .
Related
I have a project where i have a decent amount of Eloquent Models,
on most of the pages I need to use multiple models and their functions to build the page correctly.
I am used to this syntax:
use App\CustomFolder\CustomModel;
CustomModel::all(); // or whatever function / data I need
Though as said I have about 27 Models now and I need multiple ones on every page. So I created a blade layout (master page) where I link all these models, but now it seems these Models can't be called from the view itself.
So how can I either:
Make these Models globally available (preferred)
Make it so the models that I call (use Model;) in the layout are also available from my view.
You actually should get all your resources in the controller methods and not in your views. This goes against the MVC pattern Laravel and Eloquent both use.
In the controllers you can add and use them with the normal syntax:
use App\CustomFolder\CustomModel;
CustomModel::all(); // or whatever function / data I need
Learn more about using controllers in Laravel here.
EDIT
To not have to always type Use Path/To/Model in every controller, you can add the models to aliases in config/app.php. That way you can add for example 'CustomModel' => App\Path\To\CustomModel and use it in your controller methods as \CustomModel.
Although its a bad practice and will void MVC. But you can do like this inside php tags or {{}} braces in blade.
\App\Models\ModelName::get();
I'm new to MVC pattern and Sails. From my understanding, it seems like I should create a controller for each model. A controller is like one page.
So, if I want create a page containing product category, product list and user authentication, there are 3 models in my page. How should I structure the controllers?
You don't have create separate controllers for each model.
Controllers handle the request from the client and return views, JSON, etc.
You may be being influenced by how sails documentation explains things due to their blueprints.
Mvc does not require a 1 to 1 of controller to model. Your controller should take care of massing the data and rules needed to show your view or a group of different views. A controller may have a link to a group of your functionality e.g. user management where you handle registrations, login, forgotten credentials etc. This may require a few different models and not just your user model. It may also display a number of different views too.
Sails.js comes with blueprints enabled which means a user model will have create,update,find,findone etc against a user model. Infact sails doesnt need a controller in this instance but you could make one to over ride logic or add additional logic. There is however nothing stopping you calling a user.find method in the user controller as well as a pets.find. Just think of the controller as the conductor telling and calling everything to fire and bringing this information together to push into a view or number of views.
If i use the MVC pattern to create my Spring project, is it wrong to call the Controller from the View?
Is this schema right?:
View calls the Controller
Controller performs operations and put data result into the Model
View reads data from the Model
Edit:
In my index jsp there is a menu with several categories of articles. I want to pass the name of the category to the controller. The controller calls the method of a beans which executes a query and returns the list of articles presents into the database.The Controller puts this list into the model and the View read this list from the Model.
Thanks
What you say (in your comments) is not specially wrong, but it does not make sense.
Either the categories are known when you build the view, and then it is the controller role to collate all information and put it into the model before calling the view with the model.
Or the category is chosen through a user interaction. But at this moment, the JSP is over for a long time : the response has been committed and transmitted to the browser. The only possibility is to prepare a new request (with a form or with ajax), send this new request to the server, where it will be handled by a controller, which will collate data into a (new) model and pass it all to a view
Depends what you mean by calling. But yes, View doesn't know anything about the controllers. It sends HttpRequests, and than the mechanism doing what you describe kicks in. There's the famous schema from spring docs, basically your bullets described via diagram. The point with respect to your question is that the view doesn't call the controller rather sends the request
I think you will find your answers in article mentioned below :
http://docs.spring.io/spring-framework/docs/2.5.3/reference/mvc.html
I wrote a three cascade dropDownLists that its listData are generated from the database models.
The lists are generated with an Ajax call to action in the controller based.
I want to reuse this code and to share it with more pages.
I tried to do the following:
Write it as a Custom Widget.
currently i use 'createurl' function that calls a function in the matching controller.
I cant write JavaScript since i want to use the existing db models.
In this case i need to write the action functions in an independent file - so should i write a controller? where should i place it?
Write it as a part of a module - but it seems overkill.
any suggestions, i am sure that there is a right and simple way to do it.
You could create it as a helper. A helper is just a class in the components which has no direct action in the M->C->V action flow but can be used in any controller, model, view, component, module, etc...
I would write a helper method to call it from the controller.
Another suggestion could be to extend CController to your own base controller and have your actual controllers, extend from your custom base controller. That way you can make it easily available in every controller, and then you just set some members that contain the models to use which you set in the actual controller.
If you need more help on this, find me on freenode #yii
I'm creating templates for outgoing emails and a view seems like the best place for the template. The model is doing the handling of the actual email. So I wanted to see if I'm breaking any convention.
I would do it exactly as you are suggesting:
Load the model from the controller.
Have the model load a view as a string and set it as the message
Send the email from the model.
If you we're sending views to browser for output, I'd keep all that stuff separate, but since you're sending emails, which is more data oriented, I'd keep all my email logic in one model... template loading, sending and all.
Oh yeah, and storing you're email template in a view is the absolute best thing to do.
No - a Controller is supposed to act as an arbitrator between models and views. You might consider using a helper function instead to create the template.
In my opinion you should create your email from the controller, the model should be use only to make operations in the Database.