CakePHP: View without model from external API - cakephp

I need help directing me to the best way to accomplish this. I have a third party API that I want to use for data. I want to call the API using AJAX to display information only when necessary so that I don't have to store it in the database.
I'd like to have a separate view or element so that the view is set up and the data returned can be placed into the view.
Should I create a controller to do this? Can you have a controller without a model? I'm sure this is not an ideal MVC method but I don't really know the best way.

What you write does not make any sense at all:
I want to call the API using AJAX to display information
You do not need anything on the php server side when you do an AJAX call to a 3rd party site. Your server and Cake app is not even called in this case. Just modify the returned data as needed and injected it in the DOM tree where needed.
Can you have a controller without a model?
Yes
public $uses = array();
If you would not want to use AJAX, MVC wise it would be the best to create a Datasource for the API and use it together with a model. The example on this page shows a complete implementation of using data from a remote source.

Related

Controller Properties ASP.NET Core

Have 2 pages for one long process. Both in one controller. My problem is:
In first page, using angular - run method that generate some data. Store that data in property in controller. Move to another page, using angular - run another method and that is... my data in property is null. Is there a solution to store data in controller properties in thid situation?
New instance of a controller is created on every request so whatever you save in property of your controller this will be destroyed together with a controller when request is ended. You have to store this data either on client(cookie, local storage) or server side (session, cache, database etc.).
With ASP.Net MVC, you can't store data in a property between 2 actions.
For achieving this, you have to use cache strategy.
Please, see this link.

Save data to server using BackboneJS

I am very new to BackboneJS. I am creating a simple CRUD application using BackboneJS.
I have seen an example on the web (http://backbonetutorials.com/videos/beginner/). I can see a GET request on load of application i.e. http://backbonejs-beginner.herokuapp.com/users to get all the users in JSON format.
I just wanted to know what is this url (http://backbonejs-beginner.herokuapp.com/users).
Can someone help me to understand where and how data is getting saved?
What do I need to do, if I want to do the same thing on my localhost?
Do I need to write any server side code in order for this to work?
The url in the example is (one of) the API endpoints that the demo application interacts with in order to function. Backbone.JS will allow you to fetch and save data in many different ways (by writing a connector yourself) but the default means of getting and setting Model data for backbone is through AJAX calls to a RESTful API.
So lets take a look at this code snippet:
MyModel = Backbone.Model.extend({});
MyModelCollection = Backbone.Collection.extend({
model: MyModel,
url: '/myserver/api'
});
Note the "url" configuration option on the Collection. That is the URL that Backbone will make calls to (GET,POST and others) in order to get and set data for the application. That URL should be provided by you in most cases - especially if you are the one building the application.
So in short...yes, you will need to write some server side code for your CRUD application to work. I would suggest watching the REST tutorial I linked above and then consulting these articles which provide a bit more detail about how Backbone models are supposed to work.
What is a Model
Collection URL option
Model URL option
Backbone Model - Save
The server side code is needed.
But if you just want some data not matter they are real or fake. What don't you just create a object with fake data and set the Model or Collection with them. This is more easier than setting up a backend.

Controller logic in Element View in CakePHP

I'm working on a really big project. The aspect I'm currently working on requires that email templates are sent to a user when they're added to a learning course by another user.
The controller that deals with the request, does a bunch of str_replace tasks to find variables in the text (which the user can edit before adding another user to the learning course) and then replaces it with some values in the DB.
I took over this project and I'm not happy with the way half the things are done but time costs dictate I rather just go along with it.
The email is sent using Cake's native email function. It uses a template to capture data and send to the user.
Here's the question:
Should I keep the logic in the controller or do you think it's safe to move it to the element view's .ctp file?
My first instinct is to leave it in the controller as per the usual MVC separation ideals.
Cheers
This is a very important question - what are you using exactly for the email? The old email component or the new CakeEmail class? Which CakePHP core version are you using?
Here are some plausible aproaches here. You can:
Set all those variables, pass them to the view and do all the "replacing" there.
Encapsulate this logic in a component, attach it to your controller(s) and use it.
Just leave it in a private function within the controller and call that function whenever needed. (not really MVC)

Where is the correct location for an AJAX call in Backbone.js?

I'm learning Backbone.js for a new app I'm building.
I need to perform an AJAX call to get more properties for my model object (image, title & description of a book).
Where is the correct place for this call? In the Model, View or somewhere else? specifically related to Backbone.js MVC model.
Additional Info
I'm using LocalStorage and not a server. The AJAX call will be to a web service API and constitutes only part of my actual model data.
If you have properly configured your model you can just call model.fetch() which will issue a HTTP GET to whatever url you have configured and refresh the model with the results from the server.

mvc programming question

Am using a view file,controller and a helper. am accessing data value through webserver.
Steps:
the controller get value from webserver and set it in the view. the view uses the helper to display the data in some format. But my helper again calls the webserver method to get the inner values. Is it correct the helper accessing webservice method? Is it the correct way of programming in mvc?
Thanks,
IMO, a webservice is just another datasource and should be accessed via the model. If it's me, I handle it by either creating a new model for the service call (if the service call is in support of an existing entity, it may make more sense to make the call in that entity's model itself). My controller calls the model method, sends the data to my view which, in turn, forwards that data on to the helper.
This maintains the MVC separation, but still allows the data you need to make it's way into the helper where you need it.
I will tell you what is written in the Ruby on Rails book. I can not remember the title right now but...
Helpers are usually used for view rendering not for server calls.
Hope it helps.

Resources