how to implement a multiple models in same controller in Yii2.. need an example of it..eg. in my webserviceController having two models say users and employees. now i need to create a restful api in same controller which can access both models users and employees..
Thanks..
If you want directly create object and access it.like below
$users = new users();
$employees = new employees();
Related
Hi I am developing one restfull wep api application. I am new to the world of web api and little confused about http verbs. My tasks is to write services using web api2. I have one table in sql server and i am trying to do basic crud operation around this table. I want to send data in json format and return data as json. For example http://localhost:26079/api/User_Creation/1 returns data in json format as expected. My task is to host above method in iis so that anyone can access that method to retrieve data. I am confused suppose if i want to insert some data to db then what would be the method? I have below code in controllera and i am able to insert data.
public void Post(Noor_Users users)
{
if (ModelState.IsValid)
{
entityObject.Noor_Users.Add(users);
int result = entityObject.SaveChanges();
}
}
When i insert data my url will be http://localhost:26079/ but how can i expose my insert data method to outside world? My requirement is as follows.
URL:/user_creation
method:post
Request:parameters such as fname,lname as json
Response:0 for success 1 for failure and data(unique id assigned to each user)
may i get some help on this? Thank for consideration.
When considering REST it is important to understand and design it as you are taking actions against a resource at the location, and not like making a remote function call.
So I would suggest to have API as -
http://localhost:26079/api/User
instead of - http://localhost:26079/api/User_Creation
In order to adhere to REST principals.
Doing this I am very clearly stating that the user of the api will be able to perform operations on the resource (which is a User in this case) using different verbs viz. GET, POST, PUT and DELETE.
See some examples on using the API -
If I need to get a user with Id 1, I would call api like, http://localhost:26079/api/User/1 with GET request
To create a new user, I would call, http://localhost:26079/api/User with POST request and send the user information in request body
To delete a user with id 1, I would call http://localhost:26079/api/User/1 with Delete request
To update a user data with Id 1, call http://localhost:26079/api/User/1 with PUT request and send the updated user information in request body
Please note here that we are using just a single endpoint to perform different operations on our User by changing different HTTP verbs.
The default asp.net web api template gives some good hint on how to declare different verb methods e.g. -
public class UserController : ApiController
{
// GET api/<controller>/1
public User Get(int id)
{
}
// POST api/<controller>
public void Post([FromBody]User user)
{
}
// PUT api/<controller>/1
public void Put(int id, [FromBody]User user)
{
}
// DELETE api/<controller>/1
public void Delete(int id)
{
}
}
There is some good information here and here on designing restful api.
If you want to call your emthod from Angular, you can just use $http.post() method:
$http.post('/someUrl', data, config).then(successCallback, errorCallback);
Here you can find more information.
In all of my Spring REST Web application, I have a lot of domain objects and DTOs.
I need to filter some domain object or DTOs fields based on the spring security roles of the user who makes the request. I want Jackson to filter the output JSON to allow/disallow specific class fields to be serialized , based on the Spring GrantedAuthorities roles of the user who tries to access the resource.
I can't create new DTO for every different view combination because it would be a mess.
I have found this solution :
Spring 3.2: Filtering Jackson JSON output based on Spring Security role
But it doesn't work, the filter is only applied once, when the first user logins. Then all the other users obtain the same filtering , no matter what their role is.
I cannot explain my problem better than Ray Holland on this blog post :
http://jackson-users.ning.com/forum/topics/jackson-custom-serialization
This is the exact same problem I'm trying to solve for a few days.
I couldn't find a clean way to do that so far.
It's better to use #JsonView in spring project (example)
If #JsonView isn't enough, there isn't easy solution. It is unavoidable to define specific class(interface) to implement #JsonIgnoreProperties and #JsonFilter (
take a look Jackson: Skip Objects Conditionally )
I have two resources in my system users and organizations.
Each has a name and a relation with the other (many to many).
In my REST API I have:
/organizations - returns all organizations in the system (public)
/users - return all users in the system (public)
After this I needed to add properties to the relation (e.g. role in the organization). For this I created the concept of worker, a person in an organization.
What I have tried:
A new model, a "full resource"; it doesn't comply with a good rest design.
A nested resource inside organizations; better in json, but in Ember using EmbeddedRecordsMixin I lose the ability to manipulate the model - e.g. usage of adapter or serializer to change the resource.
How to design the REST api?
How to define Ember models and how to manipulate them?
Suggest making a 3rd resource that contains the role properties and relations to user and organization
You can use links, an example response for an organization:
{
id: 1,
links: {
users: "/organizations/1/users"
}
}
I have an ADO Entity model in my ASP.NET MVC application. In the database, I have a table with data that I would like to output to ChartJS widget to display in my view. I would like to do this using AngularJS two way data binding. I would like to use a GET statement in my controller to pull the data I need using a JsonResult. I was told not to create a new variable in the view. I have set up my connectioin to database and is working. I have referenced the following: ChartJS, AngularJS, Dx, JQuery and a few more.
I'm new to grails. I'm trying to develop a simple page with just a dropdown that when the user makes a selection an AJAX call is made to the database. Thing is that there is are no domain files at all in my application. I only have controllers and views and I intend to keep it this way. So I basically want to use grails to issue a mySQL Select statement through AJAX and get the results.
You could do the below:
From the AJAX call (I prefer jQuery), make the call to the controller action method.
In the controller's method you can use Groovy SQL to execute your query.
Return the result as JSON object and display it whatever way you want it to.
See http://groovy.codehaus.org/Tutorial+6+-+Groovy+SQL for a tutorial on executing SQL using the built-in Groovy SQL library. You can then create a controller action and map your resultset to JSON, and you won't even need a GSP view file. Here's an example Controller class that will do basically what you need.
import groovy.sql.Sql
class MyController {
def sessionFactory
def myAction() {
def sql = new Sql(sessionFactory.currentSession.connection())
sql.execute("select ....") //execute SQL using Groovy SQL
render(contentType:"application/json") {
//render your DB query results as JSON
//you could also use JsonBuilder to render JSON output
}
}
}