I am trying to imeplement a rest controller in grails 2.3.7. I have a simple controller, same actions as of a scaffolded one, nothing special.
My problem is I am not able to call show, update, delete and save actions via:
GET to localhost:8080/proj/domain/1
PUT/DELETE to localhost:8080/proj/domain/1
POST to localhost:8080/proj/domain
However it works when I add this to the url in UrlMappings.groovy
"/$controller/$action?/$id?(.$format)?"{
action = [POST:"save",GET:'show',DELETE:"delete"]
}
Im following with the grails doc's '8.1.5 Implementing REST controllers'. Based on my understanding of it, it should work without doing further configurations outside of the controller. Is modifying the url mappings necessary?
Yes adding a REST controller requires you to add a URL mapping for the resource, defining it as either singular or multi resource. Example:
“/foo”(resource:”foo”)
Or
“/foos”(resources:”foo”)
You can run url-mappings-report to see the URL mappings this produces
Related
I'm trying to create a standard WebAPI project in .NET 6. I notice that the standard ApiController route doesn't have the leading api route that I like to have. When I add it, the controller breaks. It doesn't give me a 404 (not found), it just gives me that index page that says that I have to enable javascript. I'm really at a loss at what I have to do to make my app work where I can put api at the start of my routes:
[Route("api/[controller]")]
I've tried adding it to the default controller route map in different combinations. I've tried adding the UseEndpoints method in the Program class. I'm just not sure what to differently in 6.
For those who want to "see my code", I'm just using the standard WebAPI project w/ React (NO Redux!).
Check the new setupProxy.js file image description inside the ClientApp folder, you need to configure the backend routes in the proxy config, I had a similar issue with the ASP.NET Core 6.0 with Angular project template, you can find more information here link 1 link 2
In laravel when using Route::resource() the controller contains 7 methods. I am unsure what the differences are between the edit and update methods / resources.
GET /resource/{resource}/edit edit resource.edit
PUT/PATCH /resource/{resource} update resource.update
In my understanding of REST, it seems laravels update implementation is fairly standard while the edit route I can't see to think of a scenario to use it when returning resources as JSON.
The difference is that edit is used to return the HTML form that is used to edit the resource values (notice that it responds to GET requests), while updateis the "action" that the edit form will be submitting to, and it responds to PUT or PATCH requests.
I have a Meteor application using angular-meteor. I need now to load different angular modules depending on url. I added iron-router to my application to do so and I continue to handle routes for each module using ngRoute and anchor nav but it behaves strangely if url contains params. I made a small test case which is available here:
https://github.com/clouchtibat/iron-router-ng-route
If you click on 'truc' link and then on 'test', next routes changes will make controller be instantiated two times. It works if urls have no params.
I also tested with ui-router (in the with-ui-router branch) and the problem is the same but in addition view is duplicated.
Is this a bug in one of the two routers or is there something wrong with my implementation?
Take a look at this conversations in the angular-meteor Github issues:
https://github.com/Urigo/angular-meteor/issues/154
https://github.com/Urigo/angular-meteor/issues/493
I think it can help you with some directions.
I am also having some hard time with mixin angular-meteor and iron:router.
There are good tutorials on how to create URL's in the controllers like this one. Examples are:
$this->uriBuilder->reset()->setTargetPageUid($page_uid)->setCreateAbsoluteUri(TRUE)->build();
$this->controllerContext->getUriBuilder()->reset()->setTargetPageUid($page_uid)->setArguments(array('person'=>$person->getUid())->buildFrontendUri();
In works from the controller. But I cannot do the same work in a repository. Error log say that reset() in first example and getUriBuilder in second example is called on a non-object when done from a repository.
Any clue on how to get past that?
First of all, you shouldn't do this. For creating URIs in Extbase, you need access to the current controller context, because there are several factors that go into creating an URI (currently called controller action, current host name, selected language, ...). If you would create URIs within your repository (i.e. your data access layer), you would generate a dependency to the HTTP routing layer of the Extbase framework. This is undesirable from an architectural point of view, because it can be argued that it violates separation of concerns and creates a messy cross-dependency.
That being said, if you still want to do this (instead of generating URIs in your controller, or -- better yet -- using a Fluid viewhelper): all you need for building URIs is an instance of the UriBuilder class. Nothing's stopping you from simply passing the controller's UriBuilder instance into the repository, for example as a parameter:
public function fooAction() {
$records = $this->myRepository->findRecordsWithUri($this->uriBuilder);
}
Within your repository function, then simply use the passed UriBuilder instance, just as you would in the controller.
I have an existing domain, controller, view (auto generated for the latter) which works as expected in the browser.
It also works in curl if I have a URL of the .../user/show/1 or .../user/show ie I use an action which goes against REST principles.
So, I have added to the URLMappings file
"/rest/user/$id?"(resource: "user")
which uses the auto generated controller.
The now works with curl and .../rest/user/1 but not .../rest/user - it runs the show method rather than the index method. But .../user does go to index and return a list.
Though the browser continues to work, I notice that the URLs are of the rest/user variety.
So I get the feeling I have not really understood this. Can I have two rules in the URLMappings file that point to the same place? According to the Grails 2.4.4 docs, a url with no id will use the index() method yet ..../rest/user is not - it goes to show().
What I have works but I am pretty sure I have not done it right.
Regards,
John
I think grails creates Restful UserController for your domain. But if you already have got the UserController your own controller is used. You have to make it RESTFul. Take a look into 9.1.5.1 Extending the RestfulController super class
Also try to create urlmapping your controller manaully.