Yii2 REST ful Api how to send status code - rest

Yii2 Restful API
In the above link they mention for GET Search method , Please anyone tell me for like POST create and PUT Update ...
In main.config under rules i created like,
['class' => 'yii\rest\UrlRule', 'controller' => 'v1/lkup-access-profile','extraPatterns' => ['POST create' => 'create']],
But its comes error as Method Not Allowed. This url can only handle the following request methods: POST.

yii\rest\UrlRule will create default REST URLs and actions. There's no need to add 'extraPatterns' => ['POST create' => 'create']. It's already built in. Check out the docs http://www.yiiframework.com/doc-2.0/yii-rest-urlrule.html
Use extraPatterns to extend default routes for controller. For example
['class' => 'yii\rest\UrlRule', 'controller' => 'v1/invite',
'extraPatterns' => [
'PUT {id}/accept' => 'accept',
'PUT {id}/reject' => 'reject',
],
],

Related

Yii2: How to add separate api module in root directory?

I have yii2 basic project with working web application functionality and now i want to add api functionality for this application.Currently i have directory stucture as
root
+ api
+ config
+ modules
+ v1
+ controllers
+.htaccess
+index.php
+index.php
+.htaccess
+ web
+ config
+ controllers
...
Now i am requesting /api/v1/controller/action but is getting 404 not found as response. Anything i have to configure?
If you have set up everything correctly and added the new api alias in the common/bootstrap.php file like Yii::setAlias('api', dirname(dirname(__DIR__)) . '/api');, you need to define the rules under urlManager in order to access the specific controller action inside your api/config/main.php.
For example I have a controller inside the api/modules/v1/controller/ with the name Lead and have two actions with the name actionTest() and actionLists() and i want to be able to send POST request to the actionLists() and GET request to the actionTest() then i will define them in the UrlManager like below
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'pluralize' => false,
'controller' => 'v1/lead',
'tokens' => [
'{id}' => '<id:\\w+>'
],
'extraPatterns' => [
'POST create' => 'create',
'GET test' => 'test'
]
],
]
]
Now I can send POST request to http://example.com/api/v1/lead/list and GET request to http://example.com/api/v1/lead/test?id=1234
Note: Notice the 'pluralize'=>false for the rule if no set you will notice the controller ID lead appears in plural form as leads in the endpoint URLs. This is because yii\rest\UrlRule automatically pluralizes controller IDs when creating child URL rules. You may disable this behavior by setting yii\rest\UrlRule::$pluralize to be false.

urlManager can't find site/index in Yii2

I just create a Basic App schema in Yii 2.
I try to test RESTFull api.
The site is working, by default, but when I change the urlManager
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'cliente'],
],
],
I only get 404 error.
I lost site/index!!!
Any idea?
It is this setting:
'enableStrictParsing' => true,
Look here:
https://github.com/yiisoft/yii2/blob/master/framework/web/UrlManager.php#L323
There are no explicit url rules for the siteController defined.
Read more here:
https://www.yiiframework.com/doc/guide/2.0/en/runtime-routing#url-rules
site/index action no longer works because of this setting: 'enableStrictParsing' => true. From $enableStrictParsing documentation:
If strict parsing is enabled, the incoming requested URL must match at least one of the $rules in order to be treated as a valid request. Otherwise, the path info part of the request will be treated as the requested route.
So if you enable this setting, you need to have matching URL rule to support given URL. In your case, you have only one rule for REST endpoint, so any other URL will not work. You should either disable this setting or add rule for your main page:
'rules' => [
'' => 'site/index',
['class' => 'yii\rest\UrlRule', 'controller' => 'cliente'],
],

How to create a custom API route in SocialEngine Zend

I have created a new REST API module in SocialEngine which can be browsed via http://server_address/mymodule or http://server_address/mymodule/index. I have a controller class Mymodule_IndexController inside thecontrollers directory. It has a method indexAction in which I output some JSON response. It works.
The question is, how can I add another route and corresponding action e.g. food/browse in this module. I have already added the following routes inside manifest.php, but when I browse to http://server_address/mymodule/browse, the route is not resolved (Page not found error).
'routes' => array(
'food_general' => array(
'route' => 'advancedrestapi/:controller/:action/*',
'defaults' => array(
'module' => 'advancedrestapi',
'controller' => 'index',
'action' => 'index',
),
'reqs' => array(
'controller' => '\D+',
'action' => '\D+',
),
),
How can I introduce new custom routes and corresponding PHP method to my module?
To add a custom route, you need to add a file with the same name as your 'action' and then .tpl extension. So, for the route in question ('action'=>'browse'), you will need to have a file as application/modules/mymodule/views/scripts/index/browse.tpl. The file can be empty.
Then, you will need to add a new method to your IndexController class browseAction (action + Action). Write your logic inside the method and you will be able to access the action via http://server_address/mymodule/index/browse.

Laravel 5.5 - laravel/socialite scopes and redirect

Can somebody explain to me what are scopes in laravel/socialite and how can I define multiple redirect from services.php
I need one for sign up with facebook, and another for login with facebook
config/services.php
'facebook' => [
'client_id' => '***************',
'client_secret' => '****************',
'redirectForSignUp' => 'http://localhost:8000/register/facebook/callback',
'redirectForLogin' => 'http://localhost:8000/login/facebook/callback',
],
The solution to this is:
public function redirectToProvider($accountType, $provider)
{
return Socialite::driver($provider)
->with(['redirect_uri' => "http://localhost:8000/api/auth/{$accountType}/{$provider}/callback/"])
->redirect();
}
You could use this method to replace anything in the http request url (including scopes).
To override other things like scopes, simply:
->with([scopes => 'SCOPES HERE', redirect_url => ''])
->redirect();
Open your .env file and set following value in it bottom
FACEBOOK_CLIENT_ID=xxxxxxxxx
FACEBOOK_CLIENT_SECRET=xxxxxxx
CALLBACK_URL=http://localhost:8000/auth/facebook/callback
Then after opwn config/services.php file and set following value
'facebook' => [
'client_id' => env('FACEBOOK_CLIENT_ID'),
'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
'redirect' => env('CALLBACK_URL'),
],
Visit thiss link for full laravel/socialite configuration in laravel application
http://laravelcode.com/post/laravel54-login-with-facebook-in-laravel

Yii2 Signup Email option

How can I set up automatic email for signup activation link in yii 2.0.3 advanced application template and how link back to login page
in this new version?
Configure the Swiftmailer in common\config\main.php components array:
// for sending mail using swift mailer
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'your_host_name',
'username' => 'enter_your_username_here',
'password' => 'enter_your_password',
'port' => '25',
],
],
After this you can use the following code to send mails:
Yii::$app->mailer->compose('contact/html', ['contactForm' => $form])
->setFrom('from#domain.com')
->setTo($form->email)
->setSubject($form->subject)
->send();
contact/html is the template name which is there is common/mail folder and contactForm variable having value $form is passed to that template.
You can put the above code in your controller. But I will suggest make a commonfunction of it and use it from anywhere. Hope it helps.