谁一开始就是大神?

PHP 第44页

PHP

为什么response的headers属性不能修改Content-Type,想输出图片[2.0]

Cleverelephant阅读(111)

但是它永远是 text/html; charset=UTF-8

输出图片,可以这样:
当然路径存在之类的判断你自己要做好,如果确定图片格式是jpeg,那么可以写死mime
应该是可以的吧
如果最后是通过yii框架默认的return渲染视图输出的,就是html头,因为header会覆盖。你尝试不用return,用echo
浏览器在跨域的时候会先发出一个preflight,如果改了header。然后服务器不允许改content-type
//统一response返回json格式
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
我是想统一返回json的,百度到你的问题,我这边解决了,顺便给你一些建议

yii2.0basic图片上传不了问题[2.0]

lovelybubble阅读(109)

我的视图:

/view/ext/_form.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model app\models\Ext */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="ext-form">

    <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?> 

    <?= $form->field($model, 'image1')->fileInput() ?>

    <?= $form->field($model, 'image2')->fileInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'info')->textInput(['maxlength' => true]) ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? '创建' : '更新', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>
    <?php ActiveForm::end(); ?>
</div>

这是model
models/ext.php

<?php
namespace app\models;
use Yii;
use yii\web\UploadedFile;
/**
 * This is the model class for table "ext".
 *
 * @property integer $id
 * @property string $image1
 * @property string $image2
 * @property string $info
 */
class Ext extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'ext';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['image1', 'image2'], 'string', 'max' => 60],
            [['image1', 'image2'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],
            [['info'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'image1' => 'logo图片',
            'image2' => '微信二维码图片',
            'info' => '站点简介',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function upload()
    {        
        if ($this->validate()) 
        {
            $randName=date("Y").date("m").date("d").date("H").date("i").date("s").rand(100, 999).'-'.md5(microtime());   //生成随机文件名
            $uploadPath= 'uploads/' . $randName . '.' . $this->image1->extension;  //设置保存路径, 为 backend\web\uploads
            $this->image1->saveAs($uploadPath);  //保存文件
            return Url::to('app/web/'.$uploadPath,true); //返回文件的 url 路径,使用 Url 帮助类。
        } 
        else 
        {            
            return false;
        }
    }
}

控制器:
controllers/Extcontroller.php

<?php
namespace app\controllers;
use Yii;
use app\models\Ext;
use app\models\ExtSearch;
use yii\web\Controller;
use yii\web\UploadedFile;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

class ExtController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Ext models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new ExtSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Ext model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        ......
    }

    /**

    public function actionCreate()
    {
        $model = new Ext();
        //$model->wb_logo = UploadedFile::getInstance($model, 'image1');
        if ($model->load(Yii::$app->request->post()) ) {
            //var_dump(Yii::$app->request->post());die;
            $model->save();
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    protected function findModel($id)
    {
        if (($model = Ext::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    
    public function actionUpload()
    {
        $model = new Ext();

        if (Yii::$app->request->isPost) {
            $model->image1 = UploadedFile::getInstance($model, 'image1');
            if ($model->upload()) {
                // 文件上传成功
                return;
            }
        }
        return $this->render('upload', ['model' => $model]);
    }
}

现在就是一直成功不了

[[‘image1’, ‘image2’], ‘string’, ‘max’ => 60],
[[‘image1’, ‘image2’], ‘string’, ‘max’ => 60],

yii2公共变量在哪里设置?[2.0]

Lvysea阅读(121)

yii2高级版公共变量在哪里设置?

方式挺多的,看你习惯吧,你可以在params.php或者params-local.php配置文件中,注意当然如果在这配置文件中配置,就得注意它的加载顺序,yii2按以下的顺序读取配置:
1、common/config/params.php
2、common/config/params-local.php
3、frontend/config/params.php
4、frontend/config/params-local.php
所以,如果你的公共变量前后台都公用,那你可以配置到common/config/params.php或common/config/params-local.php,如果是前台用的到那配置到frontend/config/params.php或frontend/config/params-local.php,后台用的到那配置到backend/config/params.php或backend/config/params-local.php中。
读取则是: echo Yii::$app->params[‘adminEmail’]; 表示读取配置文件中配置的adminEmail这个变量,按照上面的顺序读取,后面的配置文件会覆盖掉前面的配置文件定义的值。如果所有配置都读取不到,返回null。
还有,我个人的习惯是自己定义个常量类,然后定义静态的属性,使用的时候通过 “类::$属性” 来读取。
怎么用还是很灵活的,没有特别的要求。就看你的使用习惯

GridView::widget显示出来的图片太大[2.0]

FionaCherry阅读(100)

[
    'attribute'=>'img',
    'format' => ['raw',],
    'value' => function($model){
        return Html::img($model->img);
    }
],

我想要让它变小一点,就修改成了

[
    'attribute'=>'img',
    'format' =>[
        'image',
        [
            'height' =>50,
            'width' => 100
        ]
    ],
    'value' => function($model) {
        return Html::img($model->img);
    }
],

可是图片不显示了,怎么回事?

参数不正确

yii2.0图片上传[2.0]

lionsuper阅读(109)

5.png
“微博图片”和“微信二维码”要做成选择图片上传的那种怎么实现啊?

图片上传,一般数据库直接存放图片的地址。yii2中,可以利用一些封装好的组件对图片进行处理。参考:http://t.cn/RVOUrZm

怎样快速知道一个类的使用方法[2.0]

Kindsea阅读(107)

<?php $form = ActiveForm::begin([
    'fieldConfig' => [
        'template' => '{error}{input}',
    ],
]); ?>

比如说这一段代码,我是看视频才知道可以这样写的,自己通过查找类文档中文文档都没有发现可以这样写的参数,
那我们如何知道这样的方法呢?难道去看框架代码!那些对yii2框架很熟悉的人他们又是怎么知道的呢?

可以去看yii2的api文档
http://www.yiiframework.com/doc-2.0/yii-widgets-activeform.html
http://www.yiiframework.com/doc-2.0/yii-widgets-activefield.html
或者直接看源码,其实看源码才是最直接的。

怎样在modelB中引用modelA中义的变量[2.0]

Neofrog阅读(122)

在model A , afterSave()函数中定义一个变量 $x,
怎样在model B 中引用$x

如果变量 $x 具有代表性意义的话,你可以在 model A 中声明一个属性。
然后在afterSave()中以变量 $x 为这个属性赋值;
接下来你就可以在model B 中

UrlManager的rules问题[2.0]

Neoyak阅读(119)

1.png

2.png

我想通过Html::a访问这个URL,一直获取不到URL的参数值,请问我的UrlManager中的rules该怎样修改?

你理解错了a的用法:你这个参数不是URL的参数,是a的参数哦;你改为:Html::a('点击about',['about','params'=>'about1','id'=>'about2']);
Html::a('点击about',['about','params'=>'about1','id'=>'about2']);

sublime显示缩进线

LvyJames阅读(127)

sublime 缩进线
在哪里设置可以显示出来。

打开 Preferences -> Settings-Default
无标题.png
在里面搜索indent_guide该关键字,会找到两个结果
1.png
此时你如果设置成和我上面截图一样的的话,就会呈现下面的结果
2.png
你会看到两个红框中的白线一个“深”,一个“浅”,“深”是由于我的鼠标放在了那个函数上,这个效果是由
“indent_guide_options”: [“draw_normal”,”draw_active”],中的”draw_active”发挥的作用,如果你改为
“indent_guide_options”: [“draw_normal”],那么呈现出来的都是“浅”的。
好了,若果有其他不太懂的再交流吧!
是有的但不明顯, 想要明顯的話
第一 : Indent Guide Color
第二 : Hide tab lines
你要的是这种效果吗?捕获.PNG
有没有人遇到过 ubuntu 环境线的sb 缩进线出问题的情况。
目前是这样的 2017-03-06 16:01:57屏幕截图.png
这个线没有跟我设置的缩进tab(4个空格)匹配,导致代码折叠出问题了

登录

找回密码

注册