skip to Main Content

Code in the view, this is being copied straight from the docs and is not working.

// app -> Cells -> DropdownCell.php

 namespace AppCells;

use CodeIgniterViewCellsCell;

class DropdownCell extends Cell
{
    protected $surveys; 
   
    public function courses($params): string
    {
        $this->surveys = model('SurveyModel');
        $courses = $this->surveys->getUserCourses(auth()->user()); 
        $data['items'] = []; 

        foreach ($courses as $c) {
            $data['items'][] = ['id' => $c->id, 'title' => $c->title];
        }
        return view('components/dropdown',$data);
 
    }
}

View:

<?= view_cell('DropdownCell::courses', ['type' => 'test']) ?>

I am getting an error:

Too few arguments to function AppCellsDropdown::courses(), 0 passed

I can’t see what is going wrong with this? The dropdown cell has been made with Spark as per the doc guidelines. This is being served by a controller extending BaseController.

I should add that the view cell works fine if I don’t use parameters, I would just like to use them…

2

Answers


  1. Chosen as BEST ANSWER

    I was trying to use the logic behind a 'simple cell' however the method I found that worked was following the instructions for 'computed cell'


  2. Here’s how you should modify your show method in your view cell:

    public function show(array $params = []): string
    {
        return "<div class="alert alert-{$params['type']}">{$params['message']}</div>";
    }
    

    By adding the $params = [] in the method signature, you provide default values to the $params array. This allows the method to be called without passing any parameters, and it will work with the default values you’ve set. However, you can still pass parameters to customize the view cell as needed, as you were trying to do in your view.

    Your view code should work as expected:

    <?= view_cell('DropdownCell::show', ['type' => 'success', 'message' => 'The user has been updated.']) ?>
    

    Make sure you have saved your view cell with the updated show method signature, and you should no longer encounter the "Too few arguments" error.

    In your code, you’re using the namespace AppCellsDropdown, but your view cell is named DropdownCell. Ensure that the namespace and class name match

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search