URL management using Yii - yii

I have url: /profile/profileBase/index where "profile" is module, "profileBase" is controller and "index" is action.
I want to url manager would accept route like: /profile/read/index
where "read" could be alias of controller.
Is there any way to do this using url manager rules?
Thanks

'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
'profile/read/index '=>'profile/profileBase/index'
),
),

You should simply add the following rule in urlManager config :
'profile/read/index'=>'profile/profileBase/index',

Related

How to allow my user to use their own external SMTP in Yii2

I have an app where I want my user to be able to add their own SMTP server, whether by Mailgun, Amazon SES, etc.
If we're taking the mailgun as an example, right now my Mailgun is set up in config/web.php like so
'mailgun' => [
'class' => 'boundstate\mailgun\Mailer',
'key' => 'MYKEY',
'domain' => 'DOMAIN',
],
Then I use the following to compose an email
Yii::$app->mailgun->compose()->setFrom($FROM)
->setReplyTo($contest_creator_email)
->setTo($email)
->setSubject($subject_line)
->setTextBody($plaintext)
->setHtmlBody($htmlemail)
->send();
How would I make it so that my user could set-up his own key instead of using mine? Is there a way to do this?
You can do this by setting mailer configuration.
Before sending email you can set mailer configuration in your api/controller.
//Set config value dynamicaly
Yii::$app->set('mailer', [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'YourHostName',
'username' => 'UserName',
'password' => 'Password',
'port' => 'Port',
'encryption' => 'Encryption'
],
]);
And send mail as below.
Yii::$app->mailer->compose()->setFrom($FROM)
->setReplyTo($contest_creator_email)
->setTo($email)
->setSubject($subject_line)
->setTextBody($plaintext)
->setHtmlBody($htmlemail)
->send();

Yii Slugs using url manager

So using yii I want to have urls like this:
mydomain.com/some-short-slug
And this url to map the following:
mydomain.com/book?bookslug=some-short-slug
How can I do this?
You need do this inside url-manager:
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'caseSensitive' => false,
'rules' => array(
...
'<slug>'=>'book/index'
...
),
Now, if url like mydomain.com/some-short-slug, you have $slug equal to "some-short-slug" in the actionIndex of BookController

How to Configure yiibooster

thanks for reading, i am wondering how to install
YiiBooster
, do i need to install
YiiBootstrap
first ?
I want to install it by hand , is it enough to extract it to extensions folder and than configure the main.php or is there something i am missing ?
Also how can i make this point to the right path
'booster' => array(
'class' => 'path.alias.to.booster.components.Booster',
),
You don't need to install bootstrap. Yiibooster includes all bootstrap files it needs. Just download Yiibooster, unpack to the extension folder and add the below to your main config file,
'booster' => array(
'class' => 'ext.yiibooster.components.Bootstrap',
'responsiveCss' => true,
),
Then add the following in the preload section of the config,
'booster',
// uncomment the following to define a path alias
// Yii::setPathOfAlias('local','path/to/local-folder');
Yii::setPathOfAlias('bootstrap', dirname(__FILE__).'/../extensions/bootstrap');
// preloading 'log' component
'preload'=>array('log','bootstrap'),
// application components
'components'=>array(
//
'bootstrap' => array(
'class' => 'bootstrap.components.Booster',
'responsiveCss' => true,
),
Rename folder name yiibooster-4.0.1 to yiibooster
Step 1:
'preload' => array(
'booster',
),
Step 2:
'booster' => array(
'class' => 'ext.yiibooster.components.Booster',
'responsiveCss' => true,
),
Note: Booster this Class name
//Use view file
<?php
$this->widget('booster.widgets.TbExtendedGridView',
array(
'filter' => $model,
'fixedHeader' => true,
'type' => 'striped bordered',
'headerOffset' => 40,
// 40px is the height of the main navigation at bootstrap
'dataProvider' =>$model->search(),
'template' => "{items}",
'columns' => array(
'id',
'firstname',
'lastname',
'age',
'address',
'email',
),
)
);
?>

friendly urls with yii history.js

currently when using history.js in my view my url looks like this
http://localhost/dev/clubs/
when i sort OR go to the next page it adds the modulesName, Clubs and relationTable to the url
http://localhost/dev/modulesName/clubs/index/?Clubs_sort=relationTable.Make.desc&page=2
how would i make it look nicer? Say something like this maybe (without the /index/ too)
http://localhost/dev/clubs/?sort=Make.desc&page=2
currently in my modules controller i have this
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('Clubs', array(
'criteria' => array(
'with' =>'Make',
),
'sort'=>array(
'defaultOrder'=>'Make.Make ASC',
'attributes'=>array(
'Make.Make'=>array(
'asc'=>'Make.Make',
'desc'=>'Make.Make DESC',
)
)
),
//for friendly url when history,js is enabled
'pagination'=>array(
'pageVar'=>'page'
)
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
and in my url manager i have this
'<action:(clubs|finance)>' => 'modulesName/<action>',
any idea how to fix this? Thanks
You can use the url manager to do this in config
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
'mypagename' => 'modulename/controllername/actionname'
//any number of url's can be changed from here and no js is needed
)
),
Here I can give any name as my page name and as many as url's I like

Yii Parameterized Hostnames. what am I missing?

I am not able to get the yii parameterized hostnames working. I am trying to display http://member.testsite.com when the user clicks on login from http://www.testsite.com.
I have the member module created with SiteController.
In my rules, I have:
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'http://member.testsite.com' => 'member/site/index',
),
In my login, I have the URL pointing to
'url'=>array('member/site/index')
When I hover over login, it shows member.testsite.com, but when I click it takes me to website-unavailable.com
When I change the rules to
'http://member.testsite.com' => 'member/<controller>/<action>',
it takes me to testsite.com/member/site/index/
Am I missing any step?
Example:
'rules' => array(
'://<member:\w+>.<host>/<controller:\w+>/<action:\w+>'
=> '<controller>/<action>',
),