【问题标题】:ArgumentCountError Too few arguments to function App\Controllers\Blog::view()ArgumentCountError 函数 App\\Controllers\\Blog::view() 的参数太少
【发布时间】:2022-08-17 18:47:23
【问题描述】:

抱歉我的英语不好,但是当我尝试打开 localhost:8080/blog 时出现问题,此消息显示

函数 App\\Controllers\\Blog::view() 的参数太少,在 C:\\xampp\\htdocs\\baru\\vendor\\codeigniter4\\framework\\system\\CodeIgniter.php 中传递了 0第 896 行,预期为 1

所以这是控制器:

 
use CodeIgniter\\Controller;
use App\\Models\\ModelsBlog;
 
class Blog extends BaseController
{
    public function index()
    {$data = [
        \'title\' => \'artikel\'
    ];
        $model = new ModelsBlog();
        if (!$this->validate([]))
        {
            $data[\'validation\'] = $this->validator;
            $data[\'artikel\'] = $model->getArtikel();
            return view(\'view_list\',$data);
        }
    }

    public function form(){
        $data = [
            \'title\' => \'Edit Form\'
        ];
        helper(\'form\');
        return view(\'view_form\', $data);
    }

    public function view($id){
        $data = [
            \'title\' => \'artikel\'
        ];
        $model = new ModelsBlog();
        $data[\'artikel\'] = $model->PilihBlog($id)->getRow();
        return view(\'view\',$data);
    }

    public function simpan(){
        $model = new ModelsBlog();
        if ($this->request->getMethod() !== \'post\') {
            return redirect()->to(\'blog\');
        }
        $validation = $this->validate([
            \'file_upload\' => \'uploaded[file_upload]|mime_in[file_upload,image/jpg,image/jpeg,image/gif,image/png]|max_size[file_upload,4096]\'
        ]);
 
        if ($validation == FALSE) {
        $data = array(
            \'judul\'  => $this->request->getPost(\'judul\'),
            \'isi\' => $this->request->getPost(\'isi\')
        );
        } else {
            $upload = $this->request->getFile(\'file_upload\');
            $upload->move(WRITEPATH . \'../public/assets/blog/images/\');
        $data = array(
            \'judul\'  => $this->request->getPost(\'judul\'),
            \'isi\' => $this->request->getPost(\'isi\'),
            \'gambar\' => $upload->getName()
        );
        }
        $model->SimpanBlog($data);
        return redirect()->to(\'./blog\')->with(\'berhasil\', \'Data Berhasil di Simpan\');
    }

    public function form_edit($id){
        $data = [
            \'title\' => \'edit artikel\'
        ];
        $model = new ModelsBlog();
        helper(\'form\');
        $data[\'artikel\'] = $model->PilihBlog($id)->getRow();
        return view(\'form_edit\',$data);
    }

    public function edit(){
        $model = new ModelsBlog();
        if ($this->request->getMethod() !== \'post\') {
            return redirect()->to(\'blog\');
        }
        $id = $this->request->getPost(\'id\');
        $validation = $this->validate([
            \'file_upload\' => \'uploaded[file_upload]|mime_in[file_upload,image/jpg,image/jpeg,image/gif,image/png]|max_size[file_upload,4096]\'
        ]);
 
        if ($validation == FALSE) {
        $data = array(
            \'judul\'  => $this->request->getPost(\'judul\'),
            \'isi\' => $this->request->getPost(\'isi\')
        );
        } else {
        $dt = $model->PilihBlog($id)->getRow();
        $gambar = $dt->gambar;
        $path = \'../public/assets/blog/images/\';
        @unlink($path.$gambar);
            $upload = $this->request->getFile(\'file_upload\');
            $upload->move(WRITEPATH . \'../public/assets/blog/images/\');
        $data = array(
            \'judul\'  => $this->request->getPost(\'judul\'),
            \'isi\' => $this->request->getPost(\'isi\'),
            \'gambar\' => $upload->getName()
        );
        }
        $model->edit_data($id,$data);
        return redirect()->to(\'./blog\')->with(\'berhasil\', \'Data Berhasil di Ubah\');
        
    }

    public function hapus($id){
        $model = new ModelsBlog();
        $dt = $model->PilihBlog($id)->getRow();
        $model->HapusBlog($id);
        $gambar = $dt->gambar;
        $path = \'../public/assets/blog/images/\';
        @unlink($path.$gambar);
        return redirect()->to(\'./blog\')->with(\'berhasil\', \'Data Berhasil di Hapus\');
    }

}

模型博客.php:

use CodeIgniter\\Model;
 
class ModelsBlog extends Model
{
    protected $table = \'artikel\';
     
    public function getArtikel()
    {
        return $this->findAll();  
    }
    public function SimpanBlog($data)
    {
        $query = $this->db->table($this->table)->insert($data);
        return $query;
    }
    public function PilihBlog($id)
    {
         $query = $this->getWhere([\'id\' => $id]);
         return $query;
    }
    public function edit_data($id,$data)
    {
        $query = $this->db->table($this->table)->update($data, array(\'id\' => $id));
        return $query;
    }
    public function HapusBlog($id)
    {
        $query = $this->db->table($this->table)->delete(array(\'id\' => $id));
        return $query;
    }
 }

这是view.php:

<body style=\"width: 70%; margin: 0 auto; padding-top: 30px;\">
    <div class=\"row\">
        <div class=\"col-lg-12 margin-tb\">
            <div class=\"pull-left\">
                <h2><?php echo $artikel->judul; ?></h2>
            </div>
        </div>
    </div>
    <hr>
    <div class=\"row\">
        <div class=\"col-lg-12\">
            <div class=\"row\">
                <?php
                        if (!empty($artikel->gambar)) {
                            echo \'<img src=\"\'.base_url(\"assets/blog/images/$artikel->gambar\").\'\" width=\"30%\">\';
                        }
                ?>
                <?php echo $artikel->isi; ?>
            </div>
        </div>
    </div>
    
</body>

我找不到任何解决此错误的方法,请帮助非常感谢

  • 什么是896行?错误说你用 0 个参数调用一个视图,但需要 1
  • @Vickel 它说: $class->{$this->method}(...$params); }

标签: php codeigniter


【解决方案1】:

让我们回顾一下您要告诉代码做什么。

首先,您调用 /blog。如果您打开了自动路由,这会将您转发到名为“博客”的控制器。

class Blog extends BaseController

而且由于您没有用任何东西扩展 URL,因此将调用 'index' 方法。

public function index()
{$data = [
    'title' => 'artikel'
];
    $model = new ModelsBlog();
    if (!$this->validate([]))
    {
        $data['validation'] = $this->validator;
        $data['artikel'] = $model->getArtikel();
        return view('view_list',$data);
    }
}

index 方法将 $data 设置为一个数组'title' => 'artikel'.然后用新的填充 $model模型博客.

class ModelsBlog extends Model

中没有定义 __construct 方法模型博客所以只是加载了类,并且与 $model 相关的特定执行在那里停止,这很好。

然后, index() 来自博客继续检查 $this->validate([]) 是否返回 false。由于没有 else 语句,如果 $this->validate([]) 返回 true,代码执行就会停止。所以我们假设 $this->validate([]) 返回 false。到目前为止一切顺利,您的代码没有什么奇怪的地方。

但是,如果 $this->validate([]) 返回 false,则告诉 index() 返回名为 view() 的函数。通常 CodeIgniter 会为您提供您设置为第一个参数的视图。但既然你也有一个博客名为“view”的方法,CodeIgniter 将尝试将请求重新路由到该方法。换句话说,您尝试提出的实际请求是:

博客::view()

并且由于您已声明 view() 接收 1 个强制参数,因此请求会触发错误。您可以通过重命名 view() 方法来解决问题博客到“show()”或“read()”之类的东西。与本机 CodeIgniter view() 函数不冲突的任何其他内容都会很好。

老实说,您正在通过 index() 函数调用中的两个参数发送,所以我有点困惑为什么生成的错误表明您提供了 0,但我希望至少您从我的回答中获得一些见解并设法解决问题.

如果有人可以提供有关此的更多信息,请随时在下面发表评论,我会将您的信息添加到答案中(如果它被接受)。

【讨论】:

    猜你喜欢
    • 2021-03-17
    • 2021-11-12
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    • 2018-10-11
    相关资源
    最近更新 更多