yii2 framework use RESTful web services without enabling pretty urls - yii2

We are working on an agile project and we have to use the restful web services on the yii2 framework. But we can't change the URL system to the pretty URL because some actions are not working with it.
I have searched in RESTful web services documentation but could not find any solution for this.
https://www.yiiframework.com/doc/guide/2.0/en/rest-quick-start
Recommended URL schema for RESTful web services:
https://www.yiiframework.com/doc/guide/2.0/en/rest-quick-start#configuring-url-rules
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'user'],
],
]
Is there any way to do this?
More info about the api controller:
My controller is under the controllers/api dir. So full path is like;
controllers/api/ContentController.php

Related

Is it possible to set the path for all frontend AJAX request

My slugs looks like this:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<lang>/<p_page>/<p_category>/<p_product>' => 'splitter/manage-request',
'<lang>/<p_page>/<p_category>' => 'splitter/manage-request',
'<lang>/<p_page>' => 'splitter/manage-request',
'<lang>/' => 'splitter/manage-request',
'<lang>' => 'splitter/manage-request',
'' => 'splitter/manage-request',
],
],
I am sending all the requests to that SplitterController where I am parsing them. But I don't want the AJAXs to be send to it. Is it possible to give them some default route? To redirect them backend for example. I couldn't find information about it in the documentation? Appreciating links also is I missed it. Thank you!
According to my experience, there are probably the following options for your reference:
the first: The easiest way is to add a beforeAction() to your SplitterController, determine if it is an ajax request in beforeAction(), and redirect to your target address, for example:
public function beforeAction()
{
if (\Yii::$app->request->isAjax) {
return \Yii::$app->response->redirect($targetUrl);
}
return parent::beforeAction($action);
}
second: You can check if the request is an ajax request in the nginx configuration and then redirect to your target address
third: If you can't set up additional routing rules and configure the ajax request address, then you can consider implementing it by customizing UrlManager::parseRequest(), just check if it is an ajax request before it starts parsing the request, then redirect to your target. address
This answer is translated from Google Translate

how to run action of frontend controller from backend in yii2

Is it possible to run action of frontend controller from backend?
this code works if called action is in backend too. can i specify in runAction that controller/action is in frontend?
Yii::$app->runAction('controller/action')
Also i' m tried something like
$c=new controller();
$s->action();
too but it seems it not working too. //new controller() need some parameters and i have no idea what it is.
The yii\base\Application object has a public property controllerNamespace, which defaults to app\\controllers. You need to change it accordingly to changing default controller namespace.
Change namespace in action:
Yii::$app->controllerNamespace = 'frontend\controllers' and use runAction
A way could be this .
In your backend application config you could create an additional 'UrlManager' component
name eg: urlManagerFrontEnd
return [
'components' => [
'urlManager' => [
// here is your backend URL rules
],
'urlManagerFrontEnd' => [
'class' => 'yii\web\urlManager',
'baseUrl' => 'http://your_path/frontend/web/index.php',
'enablePrettyUrl' => true,
'showScriptName' => false,
],
],
];
Then you should invoke following to compose front-end URL:
Yii::$app->urlManagerFrontEnd->createUrl();
and add the controller/action you prefer
remeber that
runAction()
Runs an action within this controller with the specified action ID and
parameters.
http://www.yiiframework.com/doc-2.0/yii-base-controller.html#runAction()-detail
this mean that cannot run an action of another controller or of another application ..
If you need a service then you must configure a RESTFull or you simply need a redirection you can use redirect

How to make a secure user authentication (post call) in Yii2 RESTful Web Services?

Yii2 RESTful Web Services takes POST as CREATE in ActiveController.
How can I send by POST the username/password through my API, and take this as a user authentication method, instead of a "create" action ?
Add URL rule to your config and necessary actions.
'components' => [
'urlManager' => [
'rules' => [
'POST /auth/<action:\w+>' => 'auth/<action>',
]
]
]

Where to put pretty urls configuration

Where should I put this code:
'urlManager' => [
'enablePrettyUrl' => true,
'rules' => [
// your rules go here
],
// ...
],
And what rules should I put there?
You need to put it inside application config.
Its location varies depending on template you are using (basic / advanced).
There is components section, where each framework component configured:
return [
'components' => [
'urlManager' => [
'enablePrettyUrl' => true,
'rules' => [
// your rules go here
],
// ...
],
],
];
This will prevent passing route passed as $_GET parameter r.
Note that for pretty urls you also need to add this:
`showScriptName` => false,
This will prevent showing index.php in urls.
As for rules - it's more extensive question. Its content depends on your needs.
You can configure route / group of routes / all routes.
Read more in official docs:
UrlManager $enablePrettyUrl
UrlManager $showScriptName
UrlManager $rules
Using Pretty Urls
Url Rules

Yii2 - Change jQuery Version

I want to change the jQuery version of yii2. I've installed yii2 through composer. I've read a similar question here:
Change JqueryAsset's jQuery version on certain page
But the problem I'm facing is where should I place this code? Should I place it on the view page? But I want to change the jquery version for all the pages. Is this code compatible for yii2?
You can easily customize jquery asset bundle by configuring assetManager in the application components configuration (usually config/web.php), for example if you want to use a jquery file in web/js folder :
'assetManager' => [
'bundles' => [
'yii\web\JqueryAsset' => [
'sourcePath' => null,
'basePath' => '#webroot',
'baseUrl' => '#web',
'js' => [
'js/jquery.js',
]
],
],
],
Read more : http://www.yiiframework.com/doc-2.0/guide-structure-assets.html#customizing-asset-bundles