Yii2 subdomain URL routing - apache

I have two instances. The first one is actually my WordPress page and the second one is for the app. I've created an A record that points to the IP address where the instance for the app is. Let's say it's example.com
When I open app.example.com everything looks fine except links.
This is how I format the link:
<a href="<?php echo Url::to(['site/xyz/']);>xyzy/a>
However, my links are not formatted very well.
Do you know what can be a problem?
I've found this post: Yii2 - subdomain routing but I couldn't find it useful.
Maybe I need to add Virtual Host or..?
UPDATE:
My UrlManager rules:
'rules' => [
'app.example.com/<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'' => 'site',
'logout' => 'site/logout',
'sign-up' => 'site/signup',
]
Also, the links are is as the following:
<a class=" "href="//import/">anchor</a>
Thanks everyone

Try to add some more rules like this:
'rules' => [
'app.example.com/<controller:\w+>/<id:\d+>' => '<controller>/view',
'app.example.com/<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'app.example.com/<controller:\w+>/<action:\w+>' => '<controller>/<action>',
//'app.example.com/<controller:\w+>' => '<controller>/index',
'app.example.com/<action:\w+>' => 'site/<action>',
],

Related

Yii2 remove site/index from url

I use the Yii2 framework and want to remove site/index from my URLs.
Right now I have the following URL
example.com/site/index
but would like to change it to simple
example.com
I tried the following approach Yii2 How to remove site/index and page parameter from url but it doesn't work.
Could you please suggest how it can be done?
Looking at the question also asked at Yii2: Remove controller from URL
I ran into problems and I had to use the Yii2 resources as https://yii2-cookbook.readthedocs.io/enable-pretty-urls/
and this is what I got
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<action>'=>'site/<action>',
'<controller:[\w\-]+>/<id:\d+>' => '<controller>/view',
'<controller:[\w\-]+>/<action:[\w\-]+>/<id:\d+>' => '<controller>/<action>',
'<controller:[\w\-]+>/<action:[\w\-]+' => '<controller>/<action>',
],
],
You need to add rule for empty path at the beginning of your rules:
'urlManager' => [
'rules' => [
'' => 'site/index',
// rest of rules
],
],

Can't make the UrlManager Rules work in Yii2

I understand many variables outside what I can provide could be the problem, but I'm still asking if someone had this problem and could help.
Here's my UrlManager config in the components
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => false,
'showScriptName' => true,
'rules' => [
'' => 'site/index',
'member' => 'site/login',
],
],
This url works:
http://exampler.com/web/index.php?r=site/login
This url returns a 404
http://example.com/web/index.php?r=member
**NOTE: ** I don't have any messy Nginx or Apache rules on my server like this guy had. My rules seem to be completely ignored, whatever I write in them.
$rules is ignored if you set $enablePrettyUrl to false. From $rules documentation:
The rules for creating and parsing URLs when $enablePrettyUrl is true. This property is used only if $enablePrettyUrl is true.
https://www.yiiframework.com/doc/api/2.0/yii-web-urlmanager#$rules-detail

YII2 UrlManager wrong route

On my site I have this urlManager rule:
'city/<id:\d+>-<alias:\S*>' => 'city/view',
On page of module "user", for example this https://example.com/user/profile, there is a link for rule
Url::to(['city/view', 'id' => $this->id, 'alias' => $this->alias], $absolute)
But link becomes this https://example.com/user/city/view?id=1&alias=city_alias
What I am doing wrong?
You need to add module ID in the route as well. In the simplest case it's
'user/city/<id:\d+>-<alias:\S*>' => 'user/city/view'
If there are more than one module using similar route you can use wildcard
'<module>/city/<id:\d+>-<alias:\S*>' => '<module>/city/view'
You should simply try :
Url::to(['/city/view', 'id' => $this->id, 'alias' => $this->alias], $absolute)
Read more about creating urls.

Beautifying URLs in Yii

I have following file structure
As default the url created for accessing the module content is for example
http://127.0.0.1/tmc/user/default/viewMessage
and for other controller it comes out to be
http://127.0.0.1/tmc/user/booking/index
The problem is I want to write a rule in my urlManager so that both controllers remain accessible AND i do not see default word in url as in first example.
However if i write following rules I am able to eliminate the default word but now other controllers in same module wont work. any help in this regard is appreciated
'<module:\w+>/<action:\w+>/<id:(.*?)>' => '<module>/default/<action>/<id>',
'<module:\w+>/<action:\w+>' => '<module>/default/<action>',
My current Url Manager is as follow
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
'/' => 'site/index',
'login' => 'site/login',
'user' => 'user/default/',
'<view:[a-zA-Z0-9-]+>/' => 'site/page',
),
),
Follow this Link for config settings
Refer this Link
//inside protected/modules/admin/AdminModule.php
class AdminModule extends CWebModule
{
//goes to TaskController instead of DefaultController
public $defaultController = 'Task';
...
Now your Yii application will routes to the “TaskController” if you request
index.php?r=admin
//same as requesting
index.php?r=admin/task

YII URLManager for RESTfull API

Trying to work on a RESTfull API with yii (being the first project using yii)
Having problem with getting URLManager to properly route calls:
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'caseSensitive' => false,
'rules' => array(
'api/<controller>' => array('api/<controller>/list', 'verb' => 'GET'),
'api/<controller>' => array('api/<controller>/create', 'verb' => 'POST'),
),
),
Tried working with this (this is not the full snippet, I had dispatchers for PUT/DELETE etc..
But it did not work... Being desperate, I tried even something as simple as that:
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'caseSensitive' => false,
'rules' => array(
'tezt' => array('landing/beta', 'verb' => 'GET'),
'tezt' => array('landing', 'verb' => 'POST'),
),
),
whenever I remove one of the rules, it works, but when I put both the rules in, none of them works, I get exception
exception.CHttpException.404
exception 'CHttpException' with message 'Unable to resolve the request
"tezt".' in /yii-1.1.10/web/CWebApplication.php:280
Been banging my head agains this for 2 days now. Probably seen all the samples and tutorials on URLManager on the web (although could not find a straightforward and complete explanation of the rules). But, no joy.
Am I doing something wrong? Is it my box setup maybe?
I tried this and it worked:
'rules'=>array(
//API URLs
array('api/<controller>/index', 'pattern'=>'api/<controller:\w+>', 'verb'=>'GET'),
array('api/<controller>/create', 'pattern'=>'api/<controller:\w+>', 'verb'=>'POST'),
array('api/<controller>/view', 'pattern'=>'api/<controller:\w+>/<id:\d+>', 'verb'=>'GET'),
array('api/<controller>/update', 'pattern'=>'api/<controller:\w+>/<id:\d+>', 'verb'=>'PUT, POST'),
array('api/<controller>/delete', 'pattern'=>'api/<controller:\w+>/<id:\d+>', 'verb'=>'DELETE'),
//Other URLs
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
"api" is a module configured in modules section as
'api'=>array('defaultController' => 'default',),
In REST client, you have to specify controller name, even for default controller.
I am using Yii 1.1.10 but I think Yii supports RESTful URLs since 1.1.7.
try this
'api/<controller:\w+>' => array('api/<controller>/list', 'verb' => 'GET'),
is api a module?
For anyone else who stumbles on this, it didn't work because the rules were declared using the same keys, so the latter rule overrode the former.
In the future, declare the pattern in the rule configuration array:
array(
'route',
'pattern' =>'somePattern',
'verb' =>'...',
),
array(
'another/route',
'pattern' =>'anotherPattern',
'verb' =>'...',
),