Symfony 2 - UrlGenerator::doGenerate is called before listener - exception

I want to add to the context a parameter, so when login is called I can use it in the route (similar to _locale).
I can add this piece of code in HttpUtils.php (as resetLocale), but i don't find it very clean. The reason I need it is the firewall redirection to the login controller, which I would like to have in its route a customized parameter.
My problem is that my listener is called after UrlGenerator::doGenerate is called, so I get a MissingMandatoryParametersException.
Here is my config.yml relevant code:
services:
mycompany.demobundle.listener.request:
class: MyCompany\DemoBundle\RequestListener
arguments: [#router, #security.context]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
Any idea???

Have you tried manipulating the priority option?
tags:
- { name: kernel.event_listener, event: kernel.request, priority: 0, method: onKernelRequest }

Yep, you should use priority option it can be from -255 to 255

Related

Calling a service from a custom button in homeassistant

I'm looking for help with calling a service from a custom button.
I have the following service in developer tools, and calling it works just fine.
service: remote.send_command
data:
device: Livingroom-aircon
command: "On"
target:
entity_id: remote.broadlink_rm4_pro
Hitting the call service button results in my aircon turning on.
In trying to add this function to a dashboard button I have the following
name: Livingroom Aircon
icon: mdi:fan
show_icon: true
type: custom:button-card
tap_action:
action: call-service
service: remote.send_command
data:
device: Livingroom-aircon
command: 'On'
target:
entity_id: remote.broadlink_rm4_pro
entity: remote.broadlink_rm4_pro
Now, when pressing the button I get an error saying
Failed to call service remote/send_command. required key not provided #data['command']
I've done the same thing with a normal button, and the following works...
show_name: true
show_icon: true
type: button
tap_action:
action: call-service
service: remote.send_command
data:
device: Livingroom-aircon
command: 'On'
target:
entity_id: remote.broadlink_rm4_pro
entity: remote.broadlink_rm4_pro
So, how do I do it from the custom button (later I want to add custom graphics/info/animation)
For custom:button-card to work you need to adjust more than just the type of the card. Have a look at the Github page of the project.
You need to replace data with service_data and device with entity_id:
name: Livingroom Aircon
icon: mdi:fan
show_icon: true
type: custom:button-card
tap_action:
action: call-service
service: remote.send_command
service_data: # changed
entity_id: Livingroom-aircon # changed
command: 'On'
target:
entity_id: remote.broadlink_rm4_pro
entity: remote.broadlink_rm4_pro

What is a state in <Link> component of React Router?

Here is a screenshot from their docs about <Link> component
What state do they mean? A Redux state?
How does it look like to pass a state? Like this?
pathname: '/foo',
query: {
x: this.props.x,
},
state: store.getState()
It's a piece of information that you'd like to send to the next page. Nothing to do with Redux. It's a plain object. I believe Flipkart is a very nice example of how it can be used to improve user experience:
Go to a Flipkart search page on a mobile device (or simulate one using Chrome DevTools)
Tap on one of the items
You'll see that the transition happens instantly and pieces of information like product images, title, rating and price are readily available on the product page. One way to implement that is passing the state they had already loaded on the search page onto the next one:
<Link
to={`/product/${id}`}
state={{
product,
}}
/>
And then:
function ProductPage(props) {
// Always check because state is empty on first visit
if (props.location.state.product) {
console.log(props.location.state.product);
// { id: '...', images: [...], price: { ... } }
}
}
There are two ways to pass data from one route to another via Link.
URL Parameter.
As state.
URL parameter help when the route params contain strings for example we want to route to a particular profile:
<Link to='/azheraleem'>Visit Profile</Link>
However, the later i.e. the state helps us pass data from one route to another which is complex data structure. (objects/arrays).
As per the react router documentation, in case of passing data from one route to another it can be done as per the below code sample:
<Link
to={{
pathname: "/profile",
search: "?name=azheraleem",
state: { fromDashboard: true }
}}
/>
The pathname is the link to the route while the search attribute contains the query string parameters, thus the on clicking the link the URL will form something like:
http://localhost:3000/profile?name=azheraleem.
But the state variable value can be accessed in the called route using the useLocation hook:
import { useLocation } from "react-router";
const profile() => {
let data = useLocation();
console.log(data.state.fromDashboard);
}
The the state property of the to prop is the param of pushState method of History DOM object described here
That props used in push/replace methods of router as described here for transitions to a new URL, adding a new entry in the browser history like this:
router.push('/users/12')
// or with a location descriptor object
router.push({
pathname: '/users/12',
query: { modal: true },
state: { fromDashboard: true }
})
It also mentioned here:
router.push(path)
router.push({ pathname, query, state }) // new "location descriptor"
router.replace(path)
router.replace({ pathname, query, state }) // new "location descriptor"
state is a property that's part of the object you can provide to the to prop of the <Link> component.
It is particularly useful if you want to send data from the current view to one the <Link> directs you to, without using common techniques such as setting URL parameters or using libraries, such as Redux.
There isn't much official information about the state key, but here's what I found in the source code of that component:
Links may pass along location state and/or query string parameters
in the state/query props, respectively.
So basically, it's like sending props to a component from a parent. Here, you are sending "state" from the current view to the target view. That's about it, really.
In simple term state in <Link/> component is use to pass information from one view to other view through router in form of object.On other page it can be access using prop.location.state.
(Note: on browser refresh state no longer contain information)
To pass state in Link:
<Link to={{pathname: "/second_page", state: {id: 123}}} />
To access id in second page view:
let id = props.location.state.id;
For more Link properties : React Router Link

Sencha/Extjs rest call with all parameters

I'm using ExtJs 5.1.1 and I've written a simple view with a grid, and selecting one row the corresponding model property are editable in some text fields.
When editing is completed the button 'save' call Model.save() method, which use the rest proxy configured to write the changes on the server.
The call made by the proxy are two, first is OPTIONS call to know which method are allowed, second call is a PUT.
My problem is PUT json contains only the changed attributes.
I would like that my application sends all the attributes in PUT, instead only the changed subset.
Is this a proxy configuration, or should I use another kind of proxy, like ajax?
Some code snippet:
Model:
Ext.define('myApp.model.CvModel', {
extend: 'Ext.data.Model',
alias: 'viewmodel.cv',
idProperty : 'code',
proxy: {
type: 'rest',
url: 'http://localhost:8080/CV/resource/rest/cvs/CodeSystem/Domain',
paramsAsJson: true,
reader: {
type: 'json',
rootProperty: 'Test_data'
}
},
fields: [{
...
Controller:
onSave: function () {
var selCv = this.getViewModel().get('selectedCv');
selCv.save();
....
You need to specify a writer config on your proxy with writeAllFields: true. By default it's false, and the default writer itself is just {type: 'json'}.

How to traverse this json object in Angular js?

I have this object received from the server:
Resource {$promise: Promise, $resolved: false, $get: function, $save: function, $query: function…}
$promise: Promise
$resolved: true__v:
_id: "54ed85a92908cc9c0cce7044"
about: "about"
amenities: Array[0]
length:
__proto__: Array[0]
created: "2015-02-25T08:19:53.790Z"
direction: ""
images: Array[2]
location: "lkj"
name: "resort3"
path: "uploads/projects/ASH_RESORT002"
profile_pic: "uploads/projects/ASH_RESORT002/images/2013-05-18-1452.jpg"
resort_id: "ASH_RESORT002"
room_count:
user: Object
__proto__: Resource
I am able to access the $promise and $resolved items in json using obj.$promise and $.resolved respectively. But I am unable to access the non dollar items like "images" by doing obj.images or obj.resort_id
BTW the same items are accesible in the view when used against ng-model. Something like ng-model="obj.resort_id" is successfully binding.
Please guide!
You aren't including the code that retrieves, or logs, this resource, so I'm taking a bit of a guess here. You may be running into a side-effect of console.log(). That function is asynchronous - it doesn't instantly log the "current" values of the object. However, object property access attempts ARE instant. It's possible your object looks 'resolved' when you console.log() it (because it IS, by the time that does its work) but accessing a property directly is looking at an object that is not yet resolved. You can confirm this by console.log()'ing the $resolved property.
The documentation for $resource contains the appropriate solution:
var User = $resource('/user/:userId', {userId:'#id'});
User.get({userId:123}).$promise.then(function(user) {
// access user.value here.
});
That is, you can't access User.* immediately after calling .get(). You need to "then" the promise and let it resolve.

Present related objects as strings on JSON responses on a Laravel REST API?

I'm writing a REST API on Laravel and I'm taking advantage of polymorphic relationships. So many resources have tied images. When I create the response, using eager loading like this:
return User::with('image')->find($id);
I get something like this:
{
id: 27,
image: {
id: 340,
url: "https://amazonbucket.com/2lBqWzDme.jpg",
created_at: "2015-01-28 17:21:17",
updated_at: "2015-01-28 18:28:42",
}
}
Instead, I want it to be automatically output just the url as a string like this, so it's easier on the frontend:
{
id: 27,
image: "https://amazonbucket.com/2lBqWzDme.jpg"
}
I tried to work with the Image::toJson() method but it didn't work. I Could use the User::toJson() method, but I'd had to do it on all the existing/upcoming instances that use images. Ideas?
As it happens usually, I found a better, less invasive way of doing this looking for something else:
Just add the $appends protected attribute to your model and set the getImageAttribute() method.
protected $appends = ['image'];
function getImageAttribute() {
return $this->images()->first()->url;
}
Check the bottom of http://laravel.com/docs/4.2/eloquent#converting-to-arrays-or-json.
It works like charm!
Overriding the Image::toJson() method won't help here. In this case, the truncated call stack would be User::toJson() --> User::toArray() --> Image::toArray().
So, now the problem becomes that in order to do what you want to do, you would have to override the Image::toArray() method so that it no longer returns an array, but instead returns a string containing the url value. I can't imagine this would go over too well, but I've never done it before.
If you're looking to only modify the Image class, the closest you could get would be to assign the Image::visible attribute to only include the url key. This would turn your JSON into:
{
id: 27,
image: {
url: "https://amazonbucket.com/2lBqWzDme.jpg",
}
}
Outside of that, you're looking at a solution where you'll need to modify each class that will be imageable. However, you could extract the required functionality out into a Trait or a base class that your other classes extend. In either case, you're looking to modify the toArray() method, not the toJson() method.