fetching data in view file

Is there any other way to fetch data in view file?

Normally it gets fetched by ID but i want it fetched by title of data, i have title field in my table and i want my data fetched by title with url like:


instead of


hey sorry dear i post in yii2 syntax.

In your view action pass title parameter in place of id.

public function actionView($title)

    { $dataCountry = Country::find()->where(['countryName' => $title])->one();


        return $this->render('view', [

            'model' => $dataCountry,



u also have to change in ur urlrules to hide title parameter.

'<controller:\w+>/<title:\w+>' => '<controller>',

'<controller:\w+>/<action:\w+>/<title:\w+>' => '<controller>/<action>',

'<controller:\w+>/<action:\w+>' => '<controller>/<action>',    

so that u can call that action by title



rups g

Error 404

Unable to resolve the request "this-is-a-demo".

Did not worked!

copy paste ur controller action and url rules if added.

oops sorry to say i am having a syntax error in you code, this is my action as i changed as per your syntax

	public function actionView($url)

    { $urldata = Page::find()->where(['url' => $url])->one();


        return $this->render('view', [

            'model' => $urldata,



this is url rule

'<controller:\w+>/<url:\w+>' => '<controller>',

'<controller:\w+>/<action:\w+>/<url:\w+>' => '<controller>/<action>',

'<controller:\w+>/<action:\w+>' => '<controller>/<action>', 


before i tried sorting my url and making them beautiful i had my controller like this:

public function actionView($id)







public function loadModel($id)




			throw new CHttpException(404,'The requested page does not exist.');

		return $model;


If i try getting data from title my url gets messy


so i tried sorting it out keeping a field url in my database which i guess would help me making my url