I have just worked in the front-end part alone. So I am not sure how to connect or fetch data from a database. If I am using a tomcat server will I be able to do that using PHP?
I am thinking of creating the project in eclipse. I am not sure how to connect that with PHP either.
if you will use Spring as back-end than you need to create model, controller and DAO. Also, you have to create connection to DB. In controller, you have to create method that will call query from DB. After you've created back-end you have to call http.get method inside of js.
e.g.:
$http.get('/user/retrieve/' + $scope.authorization.userName).success(function(data) {
$rootScope.getName = data;
})
.error(function(data){
$rootScope.getName = $scope.authorization.userName;
});
}
After you've created http get method, if call was successful data won't be empty, so you can store it into $scope variable.
To show values of $scope just put double curly brackets around name of $scope like this {{ getName }}
There are plenty of tutorials how to get data from mysql using Spring or php.
I'm giving you some insight by posting link of tutorial: http://websystique.com/springmvc/spring-mvc-4-angularjs-example/
Hope this will help you!
Related
I´ve got a server running laravel.
If I type in the php artisan route:list command, it displays all routes as usual.
Now if I try using Postman to execute GET, POST or DELETE requests, it doesn´t work because I´m not sure what goes into the curly brackets at the end of the URI´s. Im running a REST API btw. Can someone help me?
It is a some sort of values that is used by controllers. It could be primitive like string or int, or an id from database that will be converted by Laravel to object and injected in your controller method.
In example, after sending GET to /users/1 (route: users/{user}), in your controller you will get an access to object User, that has an id of 1 in your database.
Take a look at route parameters in docs: https://laravel.com/docs/5.8/routing#route-parameters
Any specific error it gives while accessing the route through postman.
You can specify your route as below.
Route::get('/test/{id}', function($id){
dd('ID is : ' . $id);
});
Let me know if this helps..
generally is an ID like some int or string, but you can do whatever uri you like in ur API just like web.routes.
I'm writing a PHP 5.6 application using apigility 1.0.4 and zend framework 2.3.3
with apigility I created a new reset service called drink
and created a filed called "drink_flavor".
I used the following filters:
Zend\Filter\StringToLower
Zend\Filter\StringTrim
now I use postman to test it.
so i configured the url to http://url/drink
I'm sending post data with raw json with the following text:
{"drink_flavor" : " AAA"}
as you can see i have a space at the beginning and the letters are capital.
now if my controller code i have the following:
public function create($data)
{
die(var_export($data,1));
}
so i'm just printing the data.
if I understood everything correctly instead of getting ' AAA' i should have gotten 'aaa'
because of my filters but I still got the unmodified data which is " AAA".
any ideas what's missing?
You have to pull the data from you InputFilter to get the filtered data.
So inside your listener:
// Get filtered data
$inputFilter = $this->getInputFilter();
$data = $inputFilter->getValues();
And continue to use that $data array instead.
The $data param in your create method is the raw/unfiltered POST data.
You should be careful using that $data as a source for your methods since anything sent by the client will be in there.
I think this is not so clearly explained in the Apigility documentation and I think that a lot of users make this mistake. I wrote about this in an issue on GitHub.
I'm creating an API using Cakephp 2.x that needs a POST request to post some data to the server however when I'm posting (using Postman) to 127.0.0.1/appname/api/confirm with code=123 in the post parameters my $_POST is an empty array.
My route works, I can see variables that I declare and output within the controller, and I've checked that the parameters are being passed in the request by using the chrome developer console checking the network data.
Router::connect('/api/confirm', array('controller' => 'awesomeController', 'action' => 'confirm'));
<?php
class AwesomeController extends AppController {
public function confirm() {
$this->autoRender = false;
$this->layout = 'ajax';
pr($_POST);
}
}
?>
I've got my endpoints for the get requests to work just fine, it only seems to be POST data.
Not quite sure why $_POST wouldn't even be available and I'm sure it's something ridiculously silly I've overlooked!
** Edit **
I've attempted the following without success:
$this->request->query
$this->request->data
$this->request->params
I have another method whereby I use GET along with ?parameter=value etc and I am able to use one of the above calls to retrieve the data.
In this case, the variables should be in
$this->request->query
Try using URLs like api/confirm?code=123, and they will be in request->query
I may be wrong since I am pretty new to cakePHP but since you set:
$this->autoRender = false;
so the view is not rendered automatically to set the view to ajax layout.
Isn't it necessary to call:
$this->render();
After setting the layout as said here?
Well, hope it helps.
If anybody come here one day by googling, just had the same problem.
Had a REST Controller, called with URL /rest/something/cool.json
Method called inside RestController.php, had output, but no POST, no REQUEST.
Tried with code=123, sending direct JSON, the only way to make it works was to set Content-Type to application/json and to send actual working JSON : Cake seems to validate prior to anything, sending raw data seems useless.
I want to make an Ajax search request agains an API and get the data returned to my PHP file, right now I'm using Javascript and jQuery to do the job. But I want to let PHP do all the job, simply because I don't want my API key to be public and I may want to cash the data in a database further on. It seems that it should be simple, but I just can't figure out how to do it clean, call javascript and return or how to "integrate" it with PHP.
I am doing my PHP in the MVC pattern, like so:
Controller called from "mastercontroller/index":
class SearchController {
public function DoControl($view, $model) {
$ret = "";
$ret .= $view->GetSearchForm();
if($view->TriedToSearch()) {
if($view->GetSearchString()) {
$ret .= $model->CheckSearchString($view->GetSearchString());
} else {
// Didn't search for anything
}
} else {
// Didn't press the search button
}
return $ret;
}
}
My view is returning an HTML form, checking if submit is pressed and also returning the searchstring, that I am sending in to my Model above.
Model:
class SearchModel {
public function CheckSearchString($searchString) {
// 1. Call Googlebooks api with the searchstring
// 2. Get JSON response to return to the controller
// 3. The controller sends the data to the View for rendering
}
}
I just can't figure out how I should do it.
I'm not entirely sure, but you seem to be asking how to perform an AJAX request without JavaScript. You can't do that -- it's not possible to use the XmlHttpRequest object without JavaScript. That's, according to legend, the origin of the "J" in the AJAX name.
It sounds like you need to use REST to call specific API's. RESTful state allows you to use web services to return specific data according to a predefined API. Data can be returned in XML or JSON.
You can do this very easily with PHP's cURL implementation using whatever keys Google gives you.
See Google's Google Books API Family page for links to PHP API and sample code.
Would the below practice be useful to you? If you just want to implement ajax functionality in your code.
Simple AJAX - PHP and Javascript
I am sorry for mistaking the content. How about the two below:
simple http request example in php
PHP HTTP-Request * from another stackoverflow question
But I think ajax is main in client program meaning. At the server-side, just call it http request.
I'm working with a PHP MVC Framework. Works really well. I like the separation of the business layer (model) with the business logic (controller). But i just stumbled upon a problem. Here's the thing:
Suppose i navigate to the following url:
http://localhost/user/showall/
In this case the userController.php is called and within that file there is a method showallAction() which gets executed.
In the showallAction() method i simply do a request to a model which gets all the users for me. Something like this:
public function showallAction()
{
// create userModel object
$users = new userModel();
// get all users and assign the data to a variable which can be accessed in the view
$this->view->users = $users->getAllUsers();
// render views
$this->view->render();
}
So this method gets all the users, assigns the data returned from the userModel to a variable and i can easily work with the returned data in my view. Just a typical MVC thing.
Now here comes the problem.
I also need to create a native iphone variant. Ofcourse the looks will be totally different. So all i actually want to do is to request this url:
http://localhost/user/showall/
And that it just gives me the array (in json format) back. So i can use that for the mobile development.
But this obviously can't be done right now because the showallAction() method assumes that it is for web browser display. It doesn't echo JSON formatted, instead it simply assings the array of users to a variable.
So that means i have to create another method "showallMobileAction()" in order to get the data, but specifically for the mobile device. But this is not an elegant solution. I'm sure that are better ways...
Anyone any idea how can i solve this problem??
In your situation i would modify the routing mechanism.
It would be useful, if you could add extension at the end of URL, which represents the format you expect, like :
http://foo.bar/news/latest >> HTML document
http://foo.bar/news/latest.html >> HTML document
http://foo.bar/news/latest.rss >> you RSS feed
http://foo.bar/news/latest.json >> data in JSON format
It's a simple pattern to recognize. And you can later expand this to add .. dunno .. pdf output, or Atom feeds.
Additionally , two comments :
Model is not a type of objects. Instead it is a layer, containing objects responsible for business logic, and objects responsible for data storage/retrieval.
View should be a full blown object, to which you bind the domain objects (objects responsible for business logic).
You could pass parameters to your url:
/user/showall/json
and get the third URL segment with a custom function or a built-in one. For instance, with CodeIgniter: $this->uri->segment(3).
Some frameworks will pass the additional parameters to your method. Just try this with the URL I wrote above:
public function showallAction()
{
print_r(func_get_args());
}
I'm not familiar with PHP MVC but in general terms I'd use the "accepts" HTML header field to request the response in either "text/html" or "text/json", the controller would check for the accepts type and return the response accordingly.