I have a question about the best way to handle a requirement in laravel specifically.
I have two projects:
1 - One in laravel which does certain things for a company daily matters.
2 - The other one in nodejs, is just an api for translations.
How do i load this translations into the laravel project.
PLAN A AND B.
Plan A: Through models, specifying the $connection variable (that points to another sql connection) in the model.An then using the models to query by the key of the translation.
Plan B: Ussing Guzzle to make a rest call to the express node api for getting all the translations for that language. And then storing it in an array and get them from there.
Note that if i do it calling to the api. I might have to call that endpoint in each service that wants to translate something.Im also getting all the translations and i might need just a couple of keys to translate.
Thank you , i hope it was clear, and im sure this question hasnt been asked before in such a concrete manner.
Related
I'm new to Laravel. Maybe my questions are a little bit silly... Sorry...
The goal: There are some API resources and I need to take some selective data from them (It will be needed to make extra API queries from data which I already got before to get full sets of required values) and fill my DB tables with it (some as one to many, some as many to many).
The problem: What Laravel tools do I need to use to reach this goal? Is it factory, seeder or somewhat else? In addition I don't understand where I have to write the code for getting data from an external API and where (in terminal manually?) I have to initiate my DB tables filling.
Maybe someone could at least advise what to learn in the Laravel official documentation at first or some helpful reference to some article from which I will be clear how this process may be implemented in Laravel. I mean the tools' set and order to use them. Not the finished code implementation of course.
I will be grateful for any help. Thanks. Sorry for my non native English.
Well, there are multiple ways of achieving what you want:
One is to create a command where you are going to write code to fetch this data (using curl or any similar way). And then you manually run the command when you want with whatever arguments you want.
Other one is to use the previous step and schedule it to run in a desired time with desired arguments.
Other possible way is, if the external API can send data to a specific URL when some action occurs in that system, then you can create a normal Route and the API should point to this Route in your Laravel. It will be specific and only work for this API.
Other one is to fetch data based on an event. Let's say that, if a user register, when this is successful, you are going to fetch User info from an external API using their email (let's say you want to get a profile picture from an API and the only way to get the user picture is sending the user's email). You can do so using Events.
There are more ways, but if you don't give too much context, then this is what I can share with you !
I'm playing around with Zend 3 and I have a question.
I'm building a small dashboard for creating and viewing time sheets. I have "sheet" entities and "client" entities.
The client pages are relatively easy, however I don't know how to program to do what I want with the sheet pages.
What I want to do is: I have a sheet entity with an id, a date, a time and a database column where the id of the client is saved. In the front end I now would like to display the client's name instead of the id.
But that means I have to use the getName() function of the client module.
Usually I would create a new Client and then just ask for the name.
However after having built my application with the Zend tutorial for blog posts, with all the factories and interfaces I don't know what instance to create and where I'd get the constructor variables from.
I'm sure there's several answers for this, but this's how i do it;
1: create a strategy for hydrator which takes client repository via __construct method. on "hydrate" method, get client data from repository and put it into sheet model
2: create a callable class with name "injectClient". on __invoke method take sheet as parameter. when you got it, ask to client repository for client of that sheet. you have to join two tables here. it's good for lazy loading. on first method, you have to load client data when you load sheet. but this one, you can just call it when you exactly need.
I'm using first answer for "must have to be there" data. Like language on multi-language applications. And second one for lazy-load.
Also you can use an object mapper like doctrine. It's always using lazy-load if you don't mention opposite.
I have a question in regards to design/refactoring an existing app. My existing app is in Laravel. We have models like User, which you would imagine be linked to the Users table in the database.
Now I am planning to move this User table to an external database, which can be reached from my application with an API call.
In all the Different parts of the application there are calls to the Current User Model like User::find(Id). This wont work anymore since now Users table is not there anymore in the database.
1st Question: how to accomplish the above by changing the model reference to call an API instead of a DB table. Is there any design pattern that can be leveraged so that I dont have to change all the User::find in all of the applications.
2nd Question: How would you handle places where the table is directly referenced by joins. Like table.x = User.x
Any help/guidance would be appreciated. Thanks
What it sounds like you need is a layer that sits over your models (perhaps something similar to the Repository Pattern). This way you can abstract out your data access layer (currently Models) and make it easily swappable for another implementation (such as API requests). This is a bit more work in Laravel 4 than 5, and may possibly require significant refactoring.
As for your second question, your API will have to govern more advanced queries and have an endpoint/method for them.
I'm new to RESTful web services and still figuring out the design/architecture aspect coupled with MVC pattern. I am using Codeigniter framework to implement MVC.
I had a pretty simple question. I am using using form data to update a table in my database. I have written an api that will do this:
http://www.example.com/api/resource/tablename/?param1=info1...
Typical api. What I wanted to know was, in the MVC pattern should I be using cURL in my VIEW to POST data and update my table with the form data or should I still be send the POST data to my controller and make the api call from the controller to update the table.
To me it seems arbitrary at this point as both will accomplish the same thing but what is the standard practice? Is it okay to directly communicate with you api from the VIEW to update your db table??
Is it okay to directly communicate with you api from the VIEW to
update your db table??
Yes, it is...in fact that is pretty much what you should do in this case! Send your data directly to the API. Your API should do all data validation and return an error message (in a standardized format like JSON, XML etc) if any data validation fails OR perform whatever action it needs to do with the POSTed data. A great benefit in doing so would be that your API can be used by any caller and would be a complete ecosystem by itself.
Without knowing more about your intended applications I can say this:
Typically you want to try and keep any processing logic (PHP) out of your views if possible. The whole point of the controller is to handle transaction operations from your model and then pass it to your view. So if you are using an API to gather some data from a service that is intended to be used/manipulated in your view then the logical location for that would be in the controller.
The MVC pattern isn't a hard and fast law of X goes in Y and Y goes in Z. It is a pattern that makes it easy to extend and abstract your data gathering, processing logic, and visual layouts.
Technically depending on the application and how you planned to use it you could create a model for the API so that it could be used in multiple controllers without the need to re-write it.
I have an existing app that was built on the Yii Framework for managing wine cellars. I used the standard Gii utility to create the initial CRUD controllers/models/views. I've significantly modified the code to meet my needs for navigation, look & feel and functionality. I'm now beginning the design for a companion mobile (android) app that will need to consume and create some of the data (get a list of wines in my cellar - GET, add a new wine to the cellar - POST) that can be used while out shopping/wine-tasting. I don't intend to expose all the models/controller options as web services, only a specific set of functions.
My question is really a design issue. Should I add the API methods to the existing CRUD controllers or should I create a new "API Controller" for each of the models (or a single apiController is another option I've seen)? My thinking is separate API controllers. This would allow me to update/deploy API specific changes more easily and to logically compartmentalize that interface. This API will need to be authenticated and I will probably implement OAuth in a future release.
BTW, I've looked at the RESTFullYii extension and I haven't been able to grok exactly how it works. I'd really love to see a working example app not just code snippets.
I suggest you to create a new controller. For example ApiController. You put all your actions such as actionAddWine. So you can call this action like:
http://example.com/index.php?r=api/addWine
The advantage of doing this, is that you have all your models, and you just need to interact with your modes via REST. In order to create a simple REST api you can check the following document which is really simple explained.
How-To: Create a REST API
If you do like the mentioned article, You can send the response to client simply using _sendResponse() method.
Another thing is to do authentications and similar things which can is readily attainable using your controller's init() method.
Another way is to create a module for your application. If you want to completely divide your API from your other codes you can create a module. You can create modules with GII. The advantage of module is that you can separate your sections. For example your actions will be:
http://example.com/index.php?r=api/wine/add
in above url you are calling an action in api module, and wine controller which its name is add.
Note that you can use your models in module by importing them.
Yes, go for separate controllers. You may even want to set up a separate application that shares some of your apps models and config but also allows for a more separate feel.
Also with planning how you will version your Api, as it is likely to need to change down the road while still needing to support your older Android app