I have made a tournament system in Laravel 5.3.
Now I want to extract the core ( generating trees ) into a plugin, and make it open source.
The idea is doing the plugin, and then replace the code in my app with the plugin's code, and all the references.
I'm making a demo, so people can migrate an seed necessary objects to generate his own tournament tree.
My main concern is that in my system, I have a lot of thing that should not belong to the plugin, but they still are in the same tables.
In my models, I removed a lot of fields / functions that are not necessary in the plugin.
For instance, In my tournament model, I have a function that handle permission, because not all users can "crud" all tournaments. This function has no place in my plugin, as I will not include any policy ( this should up to each use case )
Another example should be printing the tree. In my system, I allow user to print the tree, but in my plugin is meant to be the core functions, not optional stuff.
Also, I think I should use only 1 model, I mean, I should delete my project model, and use my plugin model as they will represent the same data. So what if I remove a field / method as mentioned previously???
For my models, a solution should be that I create child object and just extend my plugins models, but it would mean change all my actual model names, is it a good approach???
How should I manage migration??? Should I include useless fields in my plugin?
Also, which User model should I use, Xoco\my-plugin\User, or App\User
Related
This question is about the "correct" design pattern, not about functional code. I want to adhere to best-practices and use the right feature in Laravel.
I have a model called Order which contains users' product orders.
Order has several columns, like which product, quantity, etc, and is stored in mysql, with a belongsTo() call to the User model.
When I place an order using the OrderController, I call an outside API that I set up using a Service class.
Here's the main part of the question:
I need to add certain fields that the API requires, but on my end are always the same, so it would make sense to me to pack these into an object of their own and just append that object to the end of my Order data before submission.
So where is the "Best" place to put this extra data? In my model? In a Service class? I'm leaning toward the service class, but that just doesn't feel right.
You have an action that gives a single or a collection of a model. So the best practice for adding some extra data to those results is using JsonResource and ResourceCollection. By using them you can easily add anything you want in the ToArray method.
Lumen doesn't have Illuminate\Http by default but you can add it to your project.
Official Http package of laravel
Eloquent: API Resources Documentation.
Laravel beginner here. So as you can guess I am creating an blog where user can create an article under a category and have different tags.
I have defined plenty of routes for articles already.
public/article/{id}/show
public/article/{id}/delete
public/article/create
So if I define a CategoriesController and a TagsController, I'm going to have to to define all of these CRUD methods in it and the routes too as:
public/categories
public/categories/{id}/delete
public/categories/create
public/tags
public/tags/create
public/tags/delete
Should I move ahead and build it this way or is there a better way to do this?
If you're talking about an administration system, you need CRUD to maintain your tags table.
In a curated system, where tags are controlled, the process of adding tags involves finding existing tags, perhaps asynchronously. You could utilize id's behind the scenes, assuming you have a relational database.
As people type some characters into the tagging portion of the UI, the system asynchronously finds a matching tag and displays it. Once that has occurred, there is no reason not to use the tag ID, and in that case, having a full set of tag routes is useful, but is not essential unless you have curated tags. For example, even if you want to use relational keys to store the relationship, you probably just need a search function which takes a tag string, searches for the matching tag, and returns that matching data.
In some systems, whatever tags are provided are added on the fly, but even in those cases, they are usually at least conformed. For example, you might want to eliminate bad words, mixed case, punctuation etc.
In short, if you need to maintain a tag table, then you will want CRUD for tags. If not, then adding tags, would just be part of the dataset that is operated on probably as part of the POST, when an article is created.
Also, just logically speaking, you shouldn't have a route for /public/article/{id}/create. It should just be /public/article/create
here are some good directions to look for
use RESTful Resource Controllers so you don't have to define each and every CRUD operation seperately. It would make your routes and controllers neat and clean. plus RESTful its a recommended nice way.
RESTful Resource Controllers
Another way is patterns. use good patterns for designing structure and behavior. here are some good reads. have a look
https://www.ibm.com/developerworks/library/os-php-designptrns/
http://shawnmc.cool/the-repository-pattern
https://sourcemaking.com/design_patterns
one nice and common way is to use repository pattern. In repository pattern the repositories work in between models and controllers.
"Repository commonly refers to a storage location, often for safety or
preservation." - Wikipedia
This would make your controllers and models simple and clean.
I'm making a leave management (HRM) website. I'm using codeignitor HMVC to build this. Following features are included in this site:
A table to display a summary of leaves.
A table for leave types like annual, MC, urgent, other...
I was thinking to create two modules for leave_summary and leave_types, but my friend told me it is useless.
According to HMVC architecture we are trying to create self contained modules for reusability. If I'm creating a different module for leave types, I should be able to reuse it and module itself needs to be self containing. But I can't use leave_types module anywhere else.
My friend asked me to put all the leave related stuff in one module called leave. This sounds strange to me as I found lots of examples people are trying to separate things out.
Do we only need to separate the modules which can be reused in the future (ex: login module, image_gallery module, profile module) and keep all others things inside a one module?
(according to the above example I have to keep everything related to leave in a one module
ex: leave_type, leave_requests, leave_summary will be placed inside the leave module)
What are the benefits I will get, if I separate the leave_type, leave_requests, leave_summary etc... into separate modules?
Will I be able to reuse them? If so How?
In HMVC model classes and other assets can be exchanged among the modules, so how can I call it a self-contained module or a separate entity as it is depending on another module?
(ex: I have to call leave_type module's model class inside the leave_summary module to show the leave type name in a table.)
I'm little lost here. Please help me to understand. Thanks a lot!
As i work lot of MVC projects. And I am agree with your friend.
May times this question arise when i used join that i have to choose in which one module i should go for write query. If you write in one model may next developer will write in another one model.
So according me it is best to keep same type of tables which are handling relation and using for same behavior use this approach like leave model, profile model etc.
I have this idea of generating an array of user-links that will depend on user-roles.
The user can be a student or an admin.
What I have in mind is use a foreach loop to generate a list of links that is only available for certain users.
My problem is, I created a helper class called Navigation, but I am so certain that I MUST NOT hard-code the links in there, instead I want that helper class to just read an object sent from somewhere, and then will return the desired navigation array to a page.
Follow up questions, where do you think should i keep the links that will only be available for students, for admins. Should i just keep them in a text-file?
or if it is possible to create a controller that passes an array of links, for example
a method in nav_controller class -> studentLinks(){} that will send an array of links to the helper class, the the helper class will then send it to the view..
Sorry if I'm quite crazy at explaining. Do you have any related resources?
From your description it seems that you are building some education-related system. It would make sense to create implementation in such way, that you can later expand the project. Seems reasonable to expect addition of "lectors" as a role later.
Then again .. I am not sure how extensive your knowledge about MVC design pattern is.
That said, in this situation I would consider two ways to solve this:
View requests current user's status from model layer and, based on the response, requests additional data. Then view uses either admin or user templates and creates the response.
You can either hardcode the specific navigation items in the templates, from which you build the response, or the lit of available navigation items can be a part of the additional information that you requested from model layer.
The downside for this method is, that every time you need, when you need to add another group, you will have to rewrite some (if not all) view classes.
Wrap the structures from model layer in a containment object (the basis of implementation available in this post), which would let you restrict, what data is returned.
When using this approach, the views aways request all the available information from model layer, but some of it will return null, in which case the template would not be applied. To implement this, the list of available navigation items would have to be provided by model layer.
P.S. As you might have noticed from this description, view is not a template and model is not a class.
It really depends on what you're already using and the scale of your project. If you're using a db - stick it there. If you're using xml/json/yaml/whatever - store it in a file with corresponding format. If you have neither - hardcode it. What I mean - avoid using multiple technologies to store data. Also, if the links won't be updated frequently and the users won't be able to customize them I'd hardcode them. There's no point in creating something very complex for the sake of dynamics if the app will be mostly static.
Note that this question doesn't quite fit in stackoverflow. programmers.stackexchange.com would probably be a better fit
I know naming conventions for tables used by plugins generally start with the name of the plugin and then the model pluralized. For example lets say I had a plugin called Poll, with a model also called PollPoll and another model called PollTag then the resulting table names would be poll_polls and poll_tags. They would also have a habtm relationship so what is the convention for that table name? I believe it would poll_poll_polls_poll_tags, although it is a little redundant it makes sense since the first poll_ represents the name of the plugin, while poll_polls and poll_tags relates to the models.
Also have any naming conventions changed for plugins in 1.3? Is the above stated correct?
Not sure about cake 1.3 (I'm not using it yet), but this if you're right this sounds like a perfectly acceptable case of breaking convention and defining the jointable, and foreign keys in the model relationships and possibly in the plugin.
Why does your plugin require a join table? Seems like a design issue. Perhaps there is a case where this is needed, but if I had a HABTM relation with a plugin, I would add a modelname column to the plugin's table, rather than have to create a new table for each model I wanted to use the plugin.
It's actually not yet a convention that "tables used by plugins generally start with the name of the plugin and then the model pluralized."
The only place that idea is introduced is in an example in the book, which actually says, "it is recommended that you name your plugin controllers something relatively unique in order to avoid namespace conflicts with parent applications ... you might want to be creative with controller names, or prepend the name of the plugin to the classname."
Your Table/Model/Controller/View names must follow normal CakePHP naming conventions, and take reasonable precautions to avoid namespace clash. So it would be perfectly fine to have a "foo_orders" table for a "foo_order" model in plugin Bar.