Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shivamyadav's avatar

how to pass a $errors array to the view file in php ?

my comment store file code

<?php

$db = new Database();


$user_id = $_POST['user_id'];
$post_id = $_POST['post_id'];
$comment = $_POST['comment'];


// validating the comment 
$errors = [];

if (strlen(trim($comment)) == 0) {
    $errors['comment'] = "Comment is required.";
} else if (strlen(trim($comment)) > 1000) {
    $errors['comment'] = "The maximum length of a comment should be 1000 characters.";
}

// storing the comment into the comments table
if(empty($errors))
{
    $comment = $db->query(
        "INSERT INTO comments (post_id, user_id, comment) 
    VALUES (:post_id, :user_id, :comment)",
        [
            'post_id' => $post_id, 'user_id' => $user_id, 'comment' => $comment
        ]
    );


    if ($comment) {
        $_SESSION['msg'] = "Commented successfully!";
        header("location: /posts/post?post={$post_id}");
    }
}else{
    // how to pass the error to that form
   

    // Redirect back to the form page
    header("location: /posts/post?post={$post_id}");
}


my routes file url route

$router->get('/posts/post', "core/Http/controllers/posts/show.php");

my Router file code

<?php
require base_path("core/middleware/Middleware.php");
class Router
{

    protected $routes = [];

    public function add($uri, $controller, $method)
    {
        $this->routes[] = [
            'uri' => $uri,
            'controller' => $controller,
            'method' => $method,
            'middleware' => null

        ];
        return $this; //return object of the $routes
    }

    public function get($uri, $controller)
    {
        return $this->add($uri, $controller, "GET");
    }

    public function post($uri, $controller)
    {
        return $this->add($uri, $controller, "POST");
    }

    public function put($uri, $controller)
    {
        return $this->add($uri, $controller, "PUT");
    }

    public function patch($uri, $controller)
    {
        return $this->add($uri, $controller, "PATCH");
    }

    public function delete($uri, $controller)
    {
        return $this->add($uri, $controller, "DELETE");
    }



    public function route($uri, $method)
    {
        foreach ($this->routes as $route) {
            if ($route['uri'] == $uri && $route['method'] == strtoupper($method)) {

                // middleware instance will auto load
                if ($route['middleware']) {

                    $middleware = Middleware::MAP[$route['middleware']];
                    // var_dump($middleware);
                    $middleware = new $middleware();
                    $middleware->handle();
                }

                return require base_path($route['controller']);
            }
        }

        // abort if uri not found 
        $this->abort(404);
    }

    public function only($key)
    {
        $this->routes[array_key_last($this->routes)]['middleware'] = $key;
        // return $this;
    }

    protected function abort($code = 404)
    {
        // http_response_code($code);
        views("status-error/{$code}.php");
        die();
    }
}
0 likes
6 replies
Bogey's avatar

From the controller, you would normally do...

return redirect(route('myPlace'))->withErrors($errorBag);

// or

Return view('myView')->withErrors($errorBag);
Snapey's avatar

This looks like a plain PHP project. What View ?

The great thing about a framework is that we all understand the same concepts.

Roll your own framework and you are literally on your own

Shivamyadav's avatar

@Snapey I mean to say that i have a dir like views/posts/allfiles .. but i am unable to understand how to pass my $errors data to that particular page/route. my that post url http://localhost:9999/posts/post/3 where 3 is the post id and its dynamic.

my post route is here

//particular post details 
$router->get('/posts/post/{id}', "core/Http/controllers/posts/show.php");

and i am getting that url id in my controller which is used to fetch the particular post with the id and with that particular post i have a comment section for the users to comment on that particular post .. now the problem is comming from this comment section is a form and my route is $router->get('/posts/post/{id}', "core/Http/controllers/posts/show.php"); this as i have mentioned above were id is a dynamic .

here is my comment section route to store it

// routes for the comment section
$router->post('/posts/post/{id}', "core/Http/controllers/comments/store.php");

and my comment form which is present on this route http://localhost:9999/posts/post/3 comment form

 <form action="" method="POST">
                    <input type="hidden" name="post_id" value="<?php echo  $params['id'] ?>">
                    <input type="hidden" name="user_id" value="<?php echo $_SESSION['user']['id'] ?>">

                    <label class="mb-5" for="">Comment</label>
                    <textarea class="w-full px-3 py-5  rounded-md border-0 ring-1 ring-gray-400 focus:ring-gray-400" name="comment" id=""></textarea>
                    <p>
                        <?php echo $errors['comment'] ?? false ?>
                    </p>
                    <div class="my-4">
                        <button class="bg-yellow-400 text-white font-semibold px-4 py-2 rounded-lg shadow-xl hover:bg-yellow-500" type="submit">
                            Post comment
                        </button>
                    </div>
                </form>
coni's avatar

If you don't return a view in the controller (in that case you can pass errors as a variable in the array) and instead return a redirect, you can pass errors to the session.

Please or to participate in this conversation.