hi,
i want to change Yii2 index with a post that fetch from database with an ID
for this reason i need to send parameters to defaultRoute for example like this:
‘defaultRoute’ => ‘posts/frontend/posts/view?id=95’,
but it cannot handle the request
What generates the ID or how does the system know what ID to use?
if i put posts/frontend/posts/view?id=95 in url i can show post with id 95
if i can put this route to defaultRoute with id i can set this post as homepage
alirz23
(Alirz23)
4
you can map your defaultRoute to posts/frontend/posts/view, then in your action you can set a default id value for id param like so
public function actionView($id = 95)
{
// ...
}
yes,
but i want to send $id=95 parameter from defaultRoute to actionView
actionView is like this:
public function actionView($id)
{
echo $id;
}
I don’t thing you can set params in the default route. You will have to do something like
'defaultRoute' => 'posts/view',
In your action
public function actionView() {
$id = 'get_your_id_somehow';
$model = $this->findModel($id);
return $this->render('view', [
'model' => $model
]);
}
That is why i asked how you generate your Id for the action to see if there was an alternative way to achieve what your current goal is.
You could also set the homeUrl instead of the default route to overide it.
'homeUrl' => 'posts/frontend/posts/view?id=95',
I don’t know if this will cause any issues but it works.
i no want to get id from user or from url
i want to show post directly
on Yii1 we can do this with defaultController property
yeah this is correct answer,
i solved the problem and share it for other uses
$testUrlExp = \yii\helpers\StringHelper::explode(Yii::$app->params['homepage'], '?');
$route = $testUrlExp[0];
$paramsExp = \yii\helpers\StringHelper::explode($testUrlExp[1], '=');
$urlManager->addRules([
[
'pattern' => '/',
'route' => $route,
'defaults' => [$paramsExp[0] => $paramsExp[1]],
],
]);
PS: homeUrl not helped me