This regarding module creation inside backend folder.Example i have created a module name as "api". And also sccessfully created controller for that modules.
here is my urlmanager code:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'rules' => [
'<module:\w+>/<controller:\w+>/<action:\w+>'=>'<module>/<controller>/<action>',
'<module:\w+><controller:\w+>/<action:update|delete>/<id:\d+>' => '<module>/<controller>/<action>',
],
]
when i access url with respect to module as "api" , controller as "country" and action as "create"
http://local2host.com/bootstrap/backend/web/index.php/api/country/create
it showing 404 Not Found error
Where i am going wrong?
The second rule will never work, Also you do not have to define the rules and the normal ones will work. So this
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
],
will make this link /website.com/core/contact/index work just fine.
But I am not sure why your route is not catching the first rule... strange. It should.
Better late then never.
For make module works you need to append it in config:
'modules' => [
......
'modulename' => [
'class' => 'app\modules\modulename\Module',
],
......
],
Related
I have following url for example:
http://mywebsite.com/testing/test
And I want it to be something like this depending on the current language:
http://mywebsite.com/en/testing/test
I have language stored in: Yii::$app->language
I tried to modify my URLManager (/config/web.php), but it doesn't work for me.
'urlManager' => [
'class' => 'yii\web\UrlManager',
'showScriptName' => false,
'enablePrettyUrl' => true,
'rules' => [
'/login' => 'site/login',
'/logout' => 'site/logout',
Yii::$app->language.'/'.'<controller:\w+>s'=>'<controller>/index',
Yii::$app->language.'/'.'<controller:\w+>/<id:\d+>/<title:\w+>'=>'<controller>/view',
Yii::$app->language.'/'.'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
Yii::$app->language.'/'.'<controller:\w+>/<action:\w+>/<id:\d+>/*'=>'<controller>/<action>',
],
],
Can You help me?
I found an extension. Even if you don't want to use it, you can look into it’s code to get an idea how it works. (Changing patterns / routing / etc.)
https://github.com/codemix/yii2-localeurls
I am building a RESTful API with Yii2 (advanced) and endpoints are working as expected apart from a custom one I need.
My urlManagerlooks like:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' =>
['class' => 'yii\rest\UrlRule', 'controller' => 'api/v1/step', 'pluralize' => false],
],
If I add a custom action into the StepController like so it will work just fine - Calling with http://example.com/api/v1/step/test
public function actionTest()
However if I want to pass an ID in through the path I will receive a 404 error - http://example.com/api/v1/step/test/1
public function actionTest($id)
Is there something I'm missing?
Edit: Adding notes that may help others.
My example above was simplified but what I wanted my URL to look like was like http://example.com/api/v1/step/test-by-foobar/1 with the called method to be public function actionTestByFoobar($id). However to get this to work you have to set the urlManager rule like the following which I didn't find obvious:
'api/v1/step/test-by-foobar/1' => 'api/v1/step/test-by-foobar',
Notice that the value is hyphenated, not in camel-case.
With your code you can pass an id like this :
http://example.com/api/v1/step/test?id=1
But if you want to do it like this:
http://example.com/api/v1/step/test/1
You should rewrite the url like below:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule',
'controller' => 'api/v1/step',
'pluralize' => false
],
/* You are missing this line below */
'api/v1/step/test/<id:\d+>' => 'api/v1/step/test'
]
],
I googled and searched here, but didnt find a solution... All that I found was subdomain to controller route... But thats not what I´m looking for...
So... here we go.
I have 2 subdomains:
sub-domain-a.site.com
sub-domain-b.site.com
And I have 2 modules:
module-a
module-b
I want Yii2 to redirect all requests to sub-domain-a to module-a and all requests to sub-domain-b to module-b...
so, if I have a controller in module-a, I just call:
sub-domain-a.site.com/controller-a-1
instead of
www.site.com/module-a/controller-a-1
same thing with sub-domain-b and module-b...
How can I accomplish this with Yii??
Thx!!
in config:
[
'components' => [
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => [
'sub-domain-a.site.com/controller-a-1' => 'module-a/controller-a-1',
],
],
],
]
In my app I have a module located in:
/frontend/modules/reports/controllers/TheModule.php/
frontend/modules/reports/controllers/views/the-module/index.php
The url for this is
frontend/web/reports/the-module/index
How can I have the url like this:
frontend/web/the-module/index ?
Because if I navigate to the module url as it is now, all the links in the main menu (which is common throughout the site) will get a /reports in their url.
Note that I am using the default url manager.
Try this in frontend configuration:
return [
// ...
'components' => [
// ...
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'the-module/<action>' => 'reports/the-module/<action>'
],
],
],
];
I'm having a problem getting my Yii2 application API setup. We have a website up and running and I've been tasked to setup a API for 3rd parties to connect to us to perform certain function calls. I've been reading the docs and did some googling and found this site that has a base setup for website and api. I installed it to see how it was setup, so I could try and apply it to my site.
I'm hitting the API section of the directory structure just fine, but I can't for the life of me figure out how the routing is supposed to work.
My directory structure is now as follows:
root
--api
----config
------main.php
----modules
------v1
--------controllers
----------SearchController.php
--------models
----------Search.php
----------ApiUser.php
--------Module.php
----runtime
----web
------assets
--assets
--commands
--config
---common
---site1
--controllers
----base
----common
----site1
--mail
--migrations
--models
--modules
--runtime
--vendor
----vendor_dirs
--views
--web
My apache config is as follows for the api alias:
Alias /api /var/www/website.com/api/web
<Directory "/var/www/webiste.com/api/web">
AllowOverride All
</Directory>
I'm confused to as to how I'm supposed to setup the url_manager section of the config file so that www.website.com/api/v1/search/do-search will hit the \api\modules\v1\controllers\SearchController::actionDoSearch() function.
My config looks as follows
'id' => 'app-api',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'modules' => [
'v1' => [
'basePath' => '#app/modules/v1',
'class' => 'api\modules\v1\Module',
'controllerNamespace' => 'api\modules\v1\controllers',
],
],
'components' => [
'user' => [
'identityClass' => 'api\v1\models\ApiUser',
'enableAutoLogin' => false,
'enableSession' => false,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning', 'info', 'trace'],
],
],
],
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => 'v1/search',
'pluralize' => false,
'extraPatterns' => [
'GET do-search' => 'do-search'
]
],
],
],
],
I've read through the routing guide on the Yii2 website, but it didn't really shed any light on the subject.
update
So after tweaking my config, I think I managed to make some headway, but I'm still not there yet.
I'm now getting the following error:
ReflectionException
Class api\modules\v1\Module does not exist
My namespace in the module is as follows:
namespace api\modules\v1;
class ApiModule extends \yii\base\Module
{
So it seems that the namespace is not registering property, or the base path for the module is not correct.
Any help explaining things, so I can better understand would be greatly appreciated.
Thanks
SUCCESS
I'm an idiot. The Module.php file was not in the v1 directory, but actually one directory down.
Add your module to modules section:
'modules' => [
'v1' => [
'class' => 'app\modules\v1\Module',
'basePath' => '#app/modules/v1',
'controllerNamespace' => 'app\modules\v1\controllers'
]
Add url rules for REST controller:
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => false,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => [ 'v1/search']
],
'GET v1/search/do-search' => 'v1/search/do-search',//actually should work without this line
When creating new controller add it to controller section in url rules.