return19931112
自动加载没有找到这个class,是用composer加载的吗?贴一下composer.json
- return19931112 2018-11-16 已签到连续签到5天,获得了20个金钱
- return19931112 2018-11-14 已签到连续签到3天,获得了15个金钱
public function actionIndex() { $dataProvider = new ActiveDataProvider(); // 记录当前路由 Url::remember(); return $this->render('index', compact('dataProvider')); } public function actionHandle() { // 业务代码 // 返回上次的路由 return $this->redirect(Url::previous()); }
这样应该也可以满足你的需求
- return19931112 2018-11-15 已签到连续签到4天,获得了20个金钱
- return19931112 赞了回答
因为你的 FileUpload 继承的 Object 类,Object 类在实例化的时候,就会调用init() 方法
public function __construct($config = []) { if (!empty($config)) { Yii::configure($this, $config); } $this->init(); }
所以你现在的报错是在new FileUpload() 就报错了, 你下面修改类的属性都还没有执行,并不是不能修改!
如果你要修改类的属性,直接在new FileUpload() 时候传递一个数组就可以了(属性对应属性值)$model = New FileUpload([ 'fileInputName' => 'name', 'allowType' => ['jpg', 'png', 'gif', 'txt'], 'mimetype' => ['image/jpeg', 'image/bmp', 'image/gif', 'image/png', 'image/pjpeg', 'image/x-png', 'text/plain'], 'savePath' => Yii::getAlias('@frontend/web/upload/file') ]);
这样就可以了!
另外:个人建议 FileUpload 需要将功能拆分,上传验证交给 model, FileUpload 只负责调用 model 验证,以及处理上传路径问题
附代码:
- 上传model
<?php namespace frontend\models; use yii\base\Model; class UploadForm extends Model { public $name; public $file; /** * 设置验证场景 * * @return array */ public function scenarios() { return [ 'name' => ['name'], 'file' => ['file'], ]; } /** * {@inheritdoc} */ public function rules() { // 我这里是偷懒的写法,就是多个上传字段,使用一个上传验证类, 只要添加验证场景和验证字段规则就好了 return [ [ ['name'], 'file', 'extensions' => ['jpg', 'png', 'gif', 'txt'], 'maxSize' => 1024100000, // 'checkExtensionByMimeType' => false, // 要不要验证 mimeTypes 类型 'mimeTypes' => ['image/jpeg', 'image/bmp', 'image/gif', 'image/png', 'image/pjpeg', 'image/x-png', 'text/plain'], 'on' => 'name', ], [ ['file'], 'image', 'extensions' => ['jpg', 'png', 'gif'], 'maxSize' => 1024100000, 'on' => 'file', ] ]; } }
- FileUpload 代码
<?php namespace frontend\models; use yii; use yii\base\Object; use yii\helpers\FileHelper; use yii\web\UploadedFile; class FileUpload extends Object { /** * @var \yii\base\Model 处理的model */ public $model; /** * @var string 上传文件表单名称 */ public $fileInputName = 'file'; /** * @var string 图片保存绝对路径 */ public $savePath; /** * @var string 文件访问路径前缀 */ public $urlPathPrefix = '/upload/images'; /** * @var UploadedFile 上传文件处理类 */ private $uploadFile; /** * @var string 文件保存路径 */ private $filePath; /** * @var string 文件访问路径 */ private $urlPath; /** * @return array|void * @throws yii\base\Exception */ public function init() { if (!$this->savePath) { $this->savePath = Yii::getAlias('@frontend/web/upload/images'); } // 目录不存在创建 $this->savePath = rtrim($this->savePath, '/'); if (!file_exists($this->savePath) && !FileHelper::createDirectory($this->savePath)) { throw new yii\base\Exception('没有权限创建' . $this->savePath); } if (empty($this->model) || !($this->model instanceof Model)) { throw new yii\base\Exception('没有传递上传文件类'); } // 上传文件接收 $strField = $this->fileInputName; $this->model->$strField = $this->uploadFile = UploadedFile::getInstanceByName($this->fileInputName); if (empty($this->uploadFile)) { throw new yii\base\Exception('没有上传文件'); } // 上传文件验证 if (!$this->model->validate()) { throw new yii\base\Exception($this->model->getFirstError($strField)); } list($path, $datePath) = $this->createDir($this->savePath); $fileName = time() . mt_rand(1000, 9999) . '.' . $this->uploadFile->getExtension(); $this->filePath = $path . $fileName; $this->urlPath = rtrim($this->urlPathPrefix, '/') . '/' . $datePath . '/' . $fileName; } /** * 根据时间创建目录 * * @param $path * * @return array * * @throws yii\base\Exception */ public function createDir($path) { $datePath = date('Y') . '/' . date('md'); $path = $path . '/' . $datePath . '/'; if (!file_exists($path)) { FileHelper::createDirectory($path); } return [$path, $datePath]; } public function save() { return $this->uploadFile->saveAs($this->filePath); } public function getUrlPath() { return $this->urlPath; } }
- 控制器调用
public function actionFileUpload() { try { $model = New FileUpload([ 'fileInputName' => 'name', 'model' => new UploadForm(['scenario' => 'name']), 'savePath' => Yii::getAlias('@frontend/web/upload/file'), 'urlPathPrefix' => '/upload/file' ]); if (!$model->save()) { throw new \Exception('上传文件失败'); } return $this->asJson([ 'code' => 1, 'msg' => '上传文件成功', 'data' => ['url' => $model->getUrlPath()] ]); } catch (\Exception $e) { return $this->asJson(['code' => 0, 'msg' => $e->getMessage()]); } }
- return19931112 回复了 return19931112 的回答
<?php public function search($params) { $tableName = static::tableName(); $query = static::find()->select([ $tableName . '.id', $tableName . '.cityname', $tableName . '.parentid', $tableName . '.population', $tableName . '.gdp', $tableName . '.year', $tableName . '.acreage', $tableName . '.popu_per_sqkm', $tableName . '.gdp_per_man', $tableName . '.abbrname',]) ->with('parent'); $dataProvider = new ActiveDataProvider([ 'query' => $query, 'pagination' => [ 'pageSize' =>20, ], 'sort'=>[ 'attributes'=>[ $tableName . .'id', $tableName . '.parentid', $tableName . '.cityname', $tableName . '.population', $tableName . '.gdp', $tableName . '.year', $tableName . '.acreage', $tableName . '.popu_per_sqkm', $tableName . '.gdp_per_man', $tableName . '.abbrname', ], 'defaultOrder'=>[$tableName . '.id'=>SORT_ASC], ], ]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['=', $tableName . '.parentid', $this->parentid]) ->andFilterWhere(['like', $tableName . '.cityname', $this->cityname]); return $dataProvider; } ?>
带上表名试试
尝试把分页设置大一些,看看还会出现两条一样的记录吗
- return19931112 回复了 return19931112 的回答
FileUpload代码贴一下,无论是模型还是控制器,都只是PHP的class,不存在模型的值只能在控制器中修改之类的问题
@clao 大兄弟,你看看你的controller代码
$model->allowType = ['jpg', 'png', 'gif', 'txt']; $model->allowType = ['image/jpeg', 'image/bmp', 'image/gif', 'image/png', 'image/pjpeg', 'image/x-png', 'text/plain'];
下面的allowType覆盖上面的allowType了,自然没办法上传.txt文件
- return19931112 2018-11-13 已签到连续签到2天,获得了10个金钱
xxx-local.php一般是给开发人员在本地使用的,同一个项目可能会存在多个开发者,不同的开发者本地配置的环境可能会不一样,比如数据库,redis等,为了避免配置被上传导致的一系列问题,xxx-local.php会默认被git屏蔽