Reputation: 33
So I'm new to PHP and I've been tasked with creating a database table, creating a model that interacts with the table, and creating a controller file that has an action function that displays all fields and data using the model. I was able to create the db table and model, but I'm having issues creating the controller. Here's what I have for the model and controller:
Model
Class Tyler extends ActiveRecord
{
public function tableName()
{
return 'tyler';
}
public function rules()
{
return array(
array('GUITAR_NUM, YEAR', 'length', 'max'=>4),
array('BRAND, MODEL, COLOR', 'length', 'max'=>20),
array('GUITAR_NUM, YEAR, BRAND, MODEL, COLOR', 'safe', on'=>'search'),
array('GUITAR_NUM, YEAR, BRAND, MODEL, COLOR', 'required'),
};
}
public function attributeLabels()
{
return array(
'GUITAR_NUM' => 'Guitar Number',
'BRAND' => 'Brand',
'MODEL' => 'Model',
'YEAR' => 'Year',
'COLOR' => 'Color',
};
}
}
?>
Controller
<?php
namespace app\controllers;
use yii\web\Controller;
use app\models\TylerModel;
Class TylerController extends Controller
{
public function actionIndex()
$data = Tyler::model()->findAll();
foreach($data as $d)
{
echo $d->name.'<br>';
}
return $this->render('index',
[
'tyler' => $tyler,
]);
};
?>
Since I used actionIndex, all the data in the table is supposed to go to a specific url, but just get a 404 error. I've tried a bunch of different things, but nothing seems to work. Feedback/help would be much appreciated!
Upvotes: 2
Views: 74
Reputation: 153
The First thing I've noticed in your code, is in you rendering call. You are sending $tyler
instead of $data
. This will surely be an error after resolving the 404 status.
About the 404 status, are you using UrlManager rules. Usually they are in main.php
file under config
directory in Yii2 project (Looks like Yii2 app to me). If it's the case, can you reply with the related code?
Upvotes: 2