How in yii2 output in url instead of mysite / product / id => mysite / product / title?
I have such a problem, I want on my site to display all the id of the product, its title in view
Here is an example - http: // localhost / yii-application / product / 1
Comes into product view with id = 1
And I want it to be so -
http: // localhost / yii-application / product / TestTitle
and went so in the view of the product
php yii2
add a comment |
I have such a problem, I want on my site to display all the id of the product, its title in view
Here is an example - http: // localhost / yii-application / product / 1
Comes into product view with id = 1
And I want it to be so -
http: // localhost / yii-application / product / TestTitle
and went so in the view of the product
php yii2
add a comment |
I have such a problem, I want on my site to display all the id of the product, its title in view
Here is an example - http: // localhost / yii-application / product / 1
Comes into product view with id = 1
And I want it to be so -
http: // localhost / yii-application / product / TestTitle
and went so in the view of the product
php yii2
I have such a problem, I want on my site to display all the id of the product, its title in view
Here is an example - http: // localhost / yii-application / product / 1
Comes into product view with id = 1
And I want it to be so -
http: // localhost / yii-application / product / TestTitle
and went so in the view of the product
php yii2
php yii2
asked Jan 20 at 11:54
Vlad EvtushenkoVlad Evtushenko
33
33
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
you need to change your action parameter and urlmanager in config file . Normally In view action we'll pass id as a parameter and using Id we'll get product information. Now we are changing instead of Id we are getting product information through Title of the product.
frontend/controllers/ProductController.php:
class ProductController extends Controller
{
public function actionView($title)
{
//Based on title getting product information
$productData = ProductModel::find ()->where(['title'=>$title])->asArray()->one();
return $this->render('view',
[
'productData'=>$productData,
]);
}
}
frontend/views/product/view.php:
your html code for display product view
frontend/config/main.php
Here we are passing title as a parameter for view acton
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'product/view/<title:w+>'=>'product/view',
-------------------------------------------
-------------------------------------------
'<controller:w+>/<action:w+>/<id:d+>' => '<controller>/<action>',
'<controller:w+>/<action:w+>/' => '<controller>/<action>',
]
]
Hope it helps
This is far more complicated in practice, because title needs to be unique in this case, and URLs will look like crap if it will contain non-latin characters. It is better to use slug approach or with combined ID and simplified title as suffix.
– rob006
Jan 20 at 22:16
@rob006 : Yes, you are right, if we are using slug it will be good, in product info anyway we need maintain slug. In that case instead of title we can slug
– Naisa purushotham
Jan 21 at 5:54
If two products have the same title, how does one produce?
– Vlad Evtushenko
Jan 21 at 9:44
for that reason we need to implement slug behavior , it will append extra numerical to existing slug , e.g test-title,test-title1...etc
– Naisa purushotham
Jan 21 at 15:22
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54276156%2fhow-in-yii2-output-in-url-instead-of-mysite-product-id-mysite-product%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
you need to change your action parameter and urlmanager in config file . Normally In view action we'll pass id as a parameter and using Id we'll get product information. Now we are changing instead of Id we are getting product information through Title of the product.
frontend/controllers/ProductController.php:
class ProductController extends Controller
{
public function actionView($title)
{
//Based on title getting product information
$productData = ProductModel::find ()->where(['title'=>$title])->asArray()->one();
return $this->render('view',
[
'productData'=>$productData,
]);
}
}
frontend/views/product/view.php:
your html code for display product view
frontend/config/main.php
Here we are passing title as a parameter for view acton
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'product/view/<title:w+>'=>'product/view',
-------------------------------------------
-------------------------------------------
'<controller:w+>/<action:w+>/<id:d+>' => '<controller>/<action>',
'<controller:w+>/<action:w+>/' => '<controller>/<action>',
]
]
Hope it helps
This is far more complicated in practice, because title needs to be unique in this case, and URLs will look like crap if it will contain non-latin characters. It is better to use slug approach or with combined ID and simplified title as suffix.
– rob006
Jan 20 at 22:16
@rob006 : Yes, you are right, if we are using slug it will be good, in product info anyway we need maintain slug. In that case instead of title we can slug
– Naisa purushotham
Jan 21 at 5:54
If two products have the same title, how does one produce?
– Vlad Evtushenko
Jan 21 at 9:44
for that reason we need to implement slug behavior , it will append extra numerical to existing slug , e.g test-title,test-title1...etc
– Naisa purushotham
Jan 21 at 15:22
add a comment |
you need to change your action parameter and urlmanager in config file . Normally In view action we'll pass id as a parameter and using Id we'll get product information. Now we are changing instead of Id we are getting product information through Title of the product.
frontend/controllers/ProductController.php:
class ProductController extends Controller
{
public function actionView($title)
{
//Based on title getting product information
$productData = ProductModel::find ()->where(['title'=>$title])->asArray()->one();
return $this->render('view',
[
'productData'=>$productData,
]);
}
}
frontend/views/product/view.php:
your html code for display product view
frontend/config/main.php
Here we are passing title as a parameter for view acton
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'product/view/<title:w+>'=>'product/view',
-------------------------------------------
-------------------------------------------
'<controller:w+>/<action:w+>/<id:d+>' => '<controller>/<action>',
'<controller:w+>/<action:w+>/' => '<controller>/<action>',
]
]
Hope it helps
This is far more complicated in practice, because title needs to be unique in this case, and URLs will look like crap if it will contain non-latin characters. It is better to use slug approach or with combined ID and simplified title as suffix.
– rob006
Jan 20 at 22:16
@rob006 : Yes, you are right, if we are using slug it will be good, in product info anyway we need maintain slug. In that case instead of title we can slug
– Naisa purushotham
Jan 21 at 5:54
If two products have the same title, how does one produce?
– Vlad Evtushenko
Jan 21 at 9:44
for that reason we need to implement slug behavior , it will append extra numerical to existing slug , e.g test-title,test-title1...etc
– Naisa purushotham
Jan 21 at 15:22
add a comment |
you need to change your action parameter and urlmanager in config file . Normally In view action we'll pass id as a parameter and using Id we'll get product information. Now we are changing instead of Id we are getting product information through Title of the product.
frontend/controllers/ProductController.php:
class ProductController extends Controller
{
public function actionView($title)
{
//Based on title getting product information
$productData = ProductModel::find ()->where(['title'=>$title])->asArray()->one();
return $this->render('view',
[
'productData'=>$productData,
]);
}
}
frontend/views/product/view.php:
your html code for display product view
frontend/config/main.php
Here we are passing title as a parameter for view acton
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'product/view/<title:w+>'=>'product/view',
-------------------------------------------
-------------------------------------------
'<controller:w+>/<action:w+>/<id:d+>' => '<controller>/<action>',
'<controller:w+>/<action:w+>/' => '<controller>/<action>',
]
]
Hope it helps
you need to change your action parameter and urlmanager in config file . Normally In view action we'll pass id as a parameter and using Id we'll get product information. Now we are changing instead of Id we are getting product information through Title of the product.
frontend/controllers/ProductController.php:
class ProductController extends Controller
{
public function actionView($title)
{
//Based on title getting product information
$productData = ProductModel::find ()->where(['title'=>$title])->asArray()->one();
return $this->render('view',
[
'productData'=>$productData,
]);
}
}
frontend/views/product/view.php:
your html code for display product view
frontend/config/main.php
Here we are passing title as a parameter for view acton
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'product/view/<title:w+>'=>'product/view',
-------------------------------------------
-------------------------------------------
'<controller:w+>/<action:w+>/<id:d+>' => '<controller>/<action>',
'<controller:w+>/<action:w+>/' => '<controller>/<action>',
]
]
Hope it helps
answered Jan 20 at 18:29
Naisa purushothamNaisa purushotham
764615
764615
This is far more complicated in practice, because title needs to be unique in this case, and URLs will look like crap if it will contain non-latin characters. It is better to use slug approach or with combined ID and simplified title as suffix.
– rob006
Jan 20 at 22:16
@rob006 : Yes, you are right, if we are using slug it will be good, in product info anyway we need maintain slug. In that case instead of title we can slug
– Naisa purushotham
Jan 21 at 5:54
If two products have the same title, how does one produce?
– Vlad Evtushenko
Jan 21 at 9:44
for that reason we need to implement slug behavior , it will append extra numerical to existing slug , e.g test-title,test-title1...etc
– Naisa purushotham
Jan 21 at 15:22
add a comment |
This is far more complicated in practice, because title needs to be unique in this case, and URLs will look like crap if it will contain non-latin characters. It is better to use slug approach or with combined ID and simplified title as suffix.
– rob006
Jan 20 at 22:16
@rob006 : Yes, you are right, if we are using slug it will be good, in product info anyway we need maintain slug. In that case instead of title we can slug
– Naisa purushotham
Jan 21 at 5:54
If two products have the same title, how does one produce?
– Vlad Evtushenko
Jan 21 at 9:44
for that reason we need to implement slug behavior , it will append extra numerical to existing slug , e.g test-title,test-title1...etc
– Naisa purushotham
Jan 21 at 15:22
This is far more complicated in practice, because title needs to be unique in this case, and URLs will look like crap if it will contain non-latin characters. It is better to use slug approach or with combined ID and simplified title as suffix.
– rob006
Jan 20 at 22:16
This is far more complicated in practice, because title needs to be unique in this case, and URLs will look like crap if it will contain non-latin characters. It is better to use slug approach or with combined ID and simplified title as suffix.
– rob006
Jan 20 at 22:16
@rob006 : Yes, you are right, if we are using slug it will be good, in product info anyway we need maintain slug. In that case instead of title we can slug
– Naisa purushotham
Jan 21 at 5:54
@rob006 : Yes, you are right, if we are using slug it will be good, in product info anyway we need maintain slug. In that case instead of title we can slug
– Naisa purushotham
Jan 21 at 5:54
If two products have the same title, how does one produce?
– Vlad Evtushenko
Jan 21 at 9:44
If two products have the same title, how does one produce?
– Vlad Evtushenko
Jan 21 at 9:44
for that reason we need to implement slug behavior , it will append extra numerical to existing slug , e.g test-title,test-title1...etc
– Naisa purushotham
Jan 21 at 15:22
for that reason we need to implement slug behavior , it will append extra numerical to existing slug , e.g test-title,test-title1...etc
– Naisa purushotham
Jan 21 at 15:22
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54276156%2fhow-in-yii2-output-in-url-instead-of-mysite-product-id-mysite-product%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown