I am trying to make a chat sandboxing api, aka creating the user, channel and adding the user to the channel. The problem is that every time i call the restApi to create a new user, it doesn't take the attributes or the friendlyName. The code is the following:
$user->chat->services($serviceSid)->users->create(
array(
'identity' => $identity,
'friendlyName' => $friendlyName,
'attributes' => $attributes,
'roleSid' => $roleSid
)
);
Thanks.
Twilio developer evangelist here.
Your code to create the user isn't quite right there. create takes the identity as the first argument and then the optional arguments in an array as the second. Try this instead:
$user->chat->services($serviceSid)->users->create(
$identity,
array(
'friendlyName' => $friendlyName,
'attributes' => $attributes,
'roleSid' => $roleSid
)
);
Let me know if that helps at all.
Related
How do I create custom methods? I understand when doing different HTTP calls such as POST, PUT, GET, DELETE, etc... but how do I create or use some custom methods aside from the main ones?
For example
get user by email?
Currently I can only get($id).. but what if I want both options? get by id, get by email?
There are several solutions for this.
You can send a http request with GET/query parameter for the user email to the collection path ('www.example.com/users'). This request should be handled inside the AbstractRestfulController its getList method and and then return a collection/array with the users that correspond to that email.
So your request for user with email some_email#example.com would look like this:
http://www.example.com/users?email=some_email%40example.com
You need to customize the getList action in a way that you can handle the query params.
Something in the controller like this:
$email = $this->params()->fromQuery('email');
Then you need to search in your database for users with that $email and then return them in an array or collection.
Another way would be to add a custom action to your controller:
public function myCustomAction(){
-- SOME CUSTOM CODE --
}
and then in router you introduce a custom route and map it to that controller action like normally in ZF2:
'my_custom_route' => array(
'type' => 'literal',
'options' => array(
'route' => '/my_custom_route',
'defaults' => array(
'controller' => 'MyControllerWithCustomAction',
'action' => 'myCustomAction',
),
),
),
This means you add a custom route for finding users by email. Even though ZF2 allows you to do this it is not according to proper Restful practice.
I use a form with a multiupload. My addAction save information regarding the downloaded files in the database, and obtains an array lastInsrtId() values. If the upload was successful, I need to be redirected to editAction, where a user can see a list of downloaded files and user can edit file attributes such as title, description and alt attribute for images for each downloaded file using this files list. How do I pass an array of values in the route? Here is my code addAction:
// upload success
$fileIds = $this->getContentService()->makeFiles($parent, $data);
return $this->redirect()->toRoute('sc-admin/file/edit', array('ids' => $fileIds));
Here is the definition for a route that should display a list of files for editing:
'edit' => array(
'type' => 'segment',
'options' => array(
'route' => '/edit[/:ids]',
'defaults' => array(
'controller' => 'sc-file',
'action' => 'edit',
),
),
),
But it generates an error for editAction
rawurlencode() expects parameter 1 to be string, array given
I do not want to use the session whenever the parameters necessary to pass an array of values, because it is a matter solely routing.
Ever seen this url?
http://foo.bar/baz?array(1=>2,3=>4)
Probably not. You got an error message so do as the error message says. This has nothing to do with Zend Framework, this is basic PHP (too many people often forget what's the core...).
array('ids' => serialize($data))
See php:serialize() and php:unserialize()
I pass a query string to SearchController::actionDefault in form of GET parameter q:
/search/?q=...
However I need to define a rule that would automatically initialize this parameter with some value or define another param.
If I'll request mysite.com/showall I need get the same content like in /search/?q=*
This is what I've tried:
'/showall' => '/search/default/index/?r=*',
I solved this!
there is possible to set defaultParams in urlManager, and finaly it looks like in application config file:
...
'components' => array(
...
'urlManager' => array(
...
'rules' => array(
....
'/show_all' => array( '/search/default/index', 'defaultParams' => array('show_all'=>'-') ),
....
),
...
),
...
),
The accepted answer also works when you are getting different requests and you need to map it to the same GET param.
For example I want all of these requests:
user/pics
user/photos
user/pictures
to actually generate: user/index?content=photos.
This might be one of a way to go:
'<controller:user>/(<content:photos>|pics|pictures)' => array('<controller>/index', 'defaultParams'=>array('content'=>'photos')),
Im using zend framework 2. I want to call a function in model from zend form.
The situation is Im having a combo box & I need to bind data from database to fill its options and value.
This is my select tag in zend form
$this->add(array(
'name' => 'ddlcountry',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Country',
'value_options' => (here I've to call function),
),
));
For this value option I want to call a function which is in model below is my function in model:
public function fetchcountry()
{
$this->adapter = $this->getServiceLocator()->get('db');
$dbAdapterConfig = $this->adapter;
$dbAdapter = $dbAdapterConfig;
$driver = $dbAdapter->getDriver();
$connection = $driver->getConnection();
$result = $connection->execute("CALL sp_showcountry()");
$statement = $result->getResource();
$resultdata = $statement->fetchAll(\PDO::FETCH_OBJ);
return $resultdata;
}
before you write such a Question, please check at least the first 10 Questions on this Page, as your Question has been asked SEVERAL Times lately ;)
Please refer to my answer provided here:
How to get data from different model for select?
Or refer to my Blogpost, which covers your problem in detail
Zend\Form\Element\Select and Database-Values
Iam new guy to Zend framework and currently Iam working on Zend2...I want to ask about Translator usage in Zend forms....If i want to use translator i directly using for labels in form view i.e.form_view.php like
$this->formLabel()->setTranslator($translator, 'date_of_birth');
But I want to add the translator at the form only i.e.in src/my_module/Form/UserForm.php
like
$this->add(array(
'name' => 'date_of_birth',
'attributes' => array(
'type' => 'text',
'id' => 'date_of_birth',
),
'options' => array(
'label' => 'DateOfBirth',
), //Here there is any option to put translator
));
Please help me...any answer would be help for me like I asked
Thanks in advance
You don't really need to do that. Since the the Translator that is set up using the factory-key translator will automatically be injected into the Form.
The best approach (in my opinion) is to make extensive use of the translator text_domain:
'translator' => array(
'locale' => 'de_DE',
'translation_file_patterns' => array(
array(
'type' => 'phparray',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.php',
'text_domain' => 'MyModuleTextDomain'
),
),
),
With this setup, the Files of your Module will automatically be inserted into the default TranslatorService which every Zend\Form knows of.
So ultimately all you have to do is make the ViewHelpers know of the TextDomain that you are using. And this is done in the following manner:
$this->formLabel()->setTranslatorTextDomain('MyModuleTextDomain');
$this->formButton()->setTranslatorTextDomain('MyModuleTextDomain');
$this->formElementErrors()->setTranslatorTextDomain('MyModuleTextDomain');
You need to do this once inside your respective view.phtml before(!) using the ViewHelpers like $this->formElement($element) or $this->formCollection($form)
And that's really all there is to it. I recall having seen a discussion somewhere about making it easier to pass along Text-Domain-Data, but i can't find it right now. So things may get a little easier in the future ;) For now, 3 lines are all that's needed though!
above answer is quite unnecessary ... as your translator was added automatically to zend form for rendering form labels and ....
only use this code in your module config :
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'phparray',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.php',
),
),
),
if u use the correct view helpers for rendering form elements (or whole form) it will automatically translated
This is not a recommended approach because forms are translated automatically if you have a translator configured (which you do if you are using the Skeleton Application). However, since you asked how to use the translator directly within your form, I will show you how you can do it. Please carefully consider if you really want to do this, as I cannot imagine a use case where it would be necessary.
To do exactly what you were asking, you can inject the translator into your form. You can do this either in your controller or in a factory. I will be using a factory in this example because it is more DRY.
// In your module's config file
'service_manager' => array(
'factories' => array(
'YourModule\Form\YourForm' => function($sm) {
$translator = $sm->get('Translator');
return new \YourModule\Form\YourForm($translator);
},
),
),
Then in your form class, you can do like this:
namespace YourModule\Form;
class RegisterForm extends \Zend\Form\Form {
public function __construct($translator) {
// Do something
$translated_string = $translator->translate('string to translate');
}
}
Then in your controller, you can do like this:
$your_form = $this->servicelocator->get('YourModule\Form\YourForm');
Or if you don't want to use the factory, you can choose to not add it and do like this instead:
$your_form = new \YourModule\Form\YourForm($this->servicelocator->get('Translator'));
I would recommend going with the factory, though.