restfullapi event calling in Yii - api

I am working on Rest Api on YII. I integrated RESTFULL extention. In this some one used this below code. What is SINGLE in below onRest Event. Can anybody explain plz.
$this->onRest('req.get.single.render', function ($id, $user_id) {
$data="hi";
$this->emitRest('req.render.json', [
[
'type' => 'raw',
'data' => $data
]
]);

It's might be called when a GET request for a single resource is to be rendered. But, I never saw SINGLE in *Yii RESTFULL extension's event. You can reference in this or this

Related

shopify api post multiple variant images on one call

I'm trying to post to shopify via the api multiple image/variants at once.
I'm currently using guzzle php and this works:
$response = $shop->api()->rest('POST', '/admin/products/#{productnumber}/images.json' ,[
"image" => [
"src" => "https://example.com/images/blahblah.jpg",
"variant_ids" => ["9999999999"]
]
However I need to do multiple ones so I'm trying to do something like this:
$response = $shop->api()->rest('POST', '/admin/products/#{productnumber}/images.json' ,[
"image" => [
"src" => "https://example.com/images/blahblah.jpg",
"variant_ids" => ["9999999999"]
],
"image" => [
"src" => "https://example.com/images/blahblah.jpg",
"variant_ids" => ["9999999999"]
]
The above does not work. Neither does this work:
$response = $shop->api()->rest('POST', '/admin/products/#{productnumber}/images.json' ,[
"image" => [
"src" => "https://example.com/images/blahblah.jpg",
"variant_ids" => ["9999999999"],
"src" => "https://example.com/images/blahblah.jpg",
"variant_ids" => ["9999999999"]
]
I can loop over each image and variant but I'm trying to avoid having to make multiple calls. Let me know please. Thank you!
I think you would have to use GraphQL and try the mutation there to add an array of images. The RestAPI is a one-trick pony it seems with one call per image. I looked through all my old codes and always burned an API call per image.
GraphQL has the concept of images[] though, so that is your best bet it seems.

Routing in module doesn't work Yii 2

I am new in Yii 2 and my problem is about routing inside a module.
I have a module in my app which is a profile cabinet both for users and admins. I created a CabinetController instead of DefaultController and also I created a AdminController and UserController.
What I want? I want this CabinetController received request and forward it to either AdminController or UserController after verify wether the user is admin or not.
In config file I set a default route for module as "cabinet"(as I understand this is a name for default controller). And in "rules" part of UrlManager I wrote following:
'modules' => [
'cabinet' => [
'class' => 'app\modules\cabinet\Module',
'defaultRoute' => 'cabinet'
],
'utility' => [
'class' => 'c006\utility\migration\Module',
],
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<_c:\w+>/' => '<_c>/',
'<_c:[\w\-]+>/<_a:\w+>' => '<_c>/<_a>',
'<_m:cabinet>/<_a:\w+>' => '<_a>',
],
],
If I go to "my-site.com/cabinet" it works fine and open "admin/index" because I made it to redirest this request to AdminController/actionIndex, but once I go to somewhere like "my-site.com/cabinet/users" it respond with 404 NotFound. I open the loger and see: exception 'yii\base\InvalidRouteException' with message 'Unable to resolve the request "cabinet/desktop"
This is my CabinetController and the way I forward requests to Admin/UserController:
public function init()
{
parent::init();
$this->user = $this->findModel();
$this->controllerToUse = $this->user->isAdmin() ? 'admin' : 'user';
}
public function actionIndex()
{
return $this->module->runAction($this->controllerToUse . '/' . $this->action->id);
}
If I change defaultAction in CabinetController it run this action normally as expected. Or if I go to "my-site.com/cabinet/admin/users" again it works good, because it found a controller in the url(as I think).
Routing can be a bit tricky in Yii2, it follows a few rules you need to understand which can be found here
But if i understand you correctly Admin/UserController is part of the Cabinet module? and you want Yii to route /cabinet/users to /cabinet/admin/users
You'll need to add some rules in your UrlManager see Rules
Example:
'rules' => [
'<module:cabinet>/<action:\w+>' => '<module>/admin/<action>',
],
Hope it helps

Using Yii2 Bootstrap widgets with Smarty

I've installed Yii2 together with Smarty for my views.
Smarty itself is working, but I can't figure out how to use the bootstrap widgets with smarty, nor find any examples.
With the default Yii renderer the widgets work great, and lots of examples are available. But when using Smarty templates documentation is nearly non existing.
How would I define this example with Smarty ?
echo Alert::widget([
'options' => [
'class' => 'alert-info',
],
'body' => 'Alert widget',
]);
Obviously the first thing would be
{use class="yii\bootstrap\Alert"}
But I can't find an example of how to define the widget itself.
You should simply try this :
{use class='#yii\bootstrap\Alert' type='function'}
{Alert body='Alert widget' options=['class' => 'alert-info']}
Read more : http://www.yiiframework.com/doc-2.0/guide-tutorial-template-engines.html#importing-static-classes-using-widgets-as-functions-and-blocks
After changing config/web.php and adding:
'globals' => ['html' => '\yii\helpers\Html'],
'uses' => ['yii\bootstrap'],
in the view section, it works.
{use class='yii\bootstrap\Alert' type='function'}
{Alert body='Alert' options=['class' => 'alert-info']}
So without the # soju suggested.

What is the correct place to set the Default Namespace for a Multiple Module project created with Phalcon DevTools?

I just started looking about Phalcon this week, I'm trying to create a multiple module application with the dev tools.
The result of running phalcon project <name> multiple only creates one module ('frontend') and it works fine. However, when I add a second module (by copying the frontend one and changing the namespace to \Backend , I couldn't get to the Backend\IndexController class.
After reading the doc page about mutiple module applications and looking at the samples (https://github.com/phalcon/mvc/tree/master/multiple and https://github.com/phalcon/mvc/tree/master/multiple-volt) and an old question on the Google group (sorry, can't post more than 2 links since I'm new on StackOverflow), I've ended commenting this this line on the services.php file:
$router->setDefaultNamespace("MyL\Frontend\Controllers"); //project name is MyL
and adding the following on the setServices of my backend/Module.php file:
$di->set('dispatcher', function() {
$dispatcher = new Dispatcher();
$dispatcher->setDefaultNamespace("MyL\Backend\Controllers");
return $dispatcher;
});
and the something similar on the frontend/Module.php
It works with these modifications, but my question is: is this the best way to do it, or is there a better way?
Thanks in advance!
You need to register your modules in your app like so:
$app = new \Phalcon\Mvc\Application();
$app->registerModules(
[
'frontend' => [
'className' => 'MyL\Frontend\Controllers',
'path' => '../apps/frontend/Module.php'
],
'backend' => [
'className' => 'MyL\Backend\Controllers',
'path' => '../apps/backend/Module.php'
],
]
);
Make sure that you have the Module.php ready also for each module
Here is a simple way to split frontend and backend in Phalcon project without modules:
https://github.com/borzov/phalcon-templates

Yii form builder

I've been working on Yii-based application. And I've faced with weird thing...
What I'm trying to do is to add input(type=file) into form. Form is created via form builder(CForm class). But input is not going to appear.
My code. Controller/action:
$model=MyModel::model()->findByPk(700);
$model->scenario='my-scenario';
$form=new CForm('path.to.forms.my-form', $model);
$this->render('view', array('form'=>$form));
View:
echo $form
Form config:
return array(
'attributes' => array(
'enctype' => 'multipart/form-data',
),
'elements' => array(
'name' => array(
'type' => 'text',
),
'image' => array(
'type' => 'file',
),
),
'buttons' => array(
'save' => array(
'type' => 'submit',
'label' => 'Save',
),
),
);
Model:
//.....
public $image;
// ....
public function rules()
{
return array(
//...
array('image', 'file', 'types'=>'png', 'on'=>'my-scenario'),
);
}
With code above I expected to see two fields - text and file. But only text one appears.
If I change file validator to, say, required - it works, but I need file validator.
I'm using Yii version 1.1.13.
The most intresting that code above works as expected with earlier Yii(1.1.9). Is this a known bug in new version? If yes - is there a solution? or do I have to rollback to previous version?
Thanks in advance.
UPDATE:
If you add a second validator (one for file and one for required) does it work?
No, it doesn't. I believe I found why. See bellow.
It seems to be caused by this line in CForm..
Yes, correct. Yesterday, armed with debugger I went deeper:)
CFormElement::getVisible() eventually calls CModel::isAttributeSafe() and CModel::getSafeAttributeNames().
CForm::getSafeAttributeNames() gets all model validators and leaves only safe ones. As we can see CFileValidator is not safe.
So, it doesn't matter how many safe validators(required or any other) have attribute assigned. CForm::getSafeAttributeNames() removes it from whitelist if there is at least one unsafe(file).
File validator is unsafe since 1.1.12 version. That is why it worked perfectly for me in 1.1.9 :)
Hence the problem is in CFileValidator(or at least connected with it) and not in CForm.
The only one solution I can see so far is creating own validator extended from CFileValidator marked safe and using it instead of built in. But I can't even imagine what problems it may cause(I believe Yii developers had a good reason for making it unsafe).
I hope this will be helpful for somebody.
UPDATE 2
array('image', 'file', 'safe'=>true, 'types'=>'png', 'on'=>'my-scenario')
this validation rule(explicit safe=true) also works.
If you add a second validator (one for file and one for required) does it work? I ran into this recently myself.
It seems to be caused by this line in CForm:
if($element->getVisible())
{
...
}
The getVisible checks the active validators for this current model scenario and leaves out any inputs that aren't used in the current validator rules. I've ended up commenting things out in our custom CForm model for our system, but if let me know if you find something that works better for you.
Issue has been posted on github
https://github.com/yiisoft/yii/issues/2089