Skip to content

Data send by the controller is not the same in the view #1253

Open
@happytodev

Description

@happytodev

Tempest version

1.0-beta.1

PHP version

8.4

Operating system

macOS

Description

Perhaps a bug with the view.
A dump at first line of view and at the third line doesn't give the same result.

Steps to reproduce

Here it is my code of BlogController.php :

    #[Get(uri: '/blog/{slug}')]
    public function show(string $slug): View
    {
        $post = $this->repository->findBySlug($slug);

        $rootPath = realpath(getcwd() . '/../');
        $markdownPath = realpath($rootPath . DIRECTORY_SEPARATOR . $post->markdown_file_path);

        $environment = new Environment();
        $environment
            ->addExtension(new CommonMarkCoreExtension())
            ->addExtension(new HighlightExtension());

        if (file_exists($markdownPath)) {
            $markdownContent = file_get_contents($markdownPath);

            // Extract frontmatter and Markdown body to avoid displaying metadata
            $parts = $this->extractFrontmatter($markdownContent);
            $markdownBody = $parts['body'];

            // Convert only the Markdown body to HTML
            $converter = new MarkdownConverter($environment);
            $content = $converter->convert($markdownBody);
            dd($content, $post);
        } else {
            $content = 'Content not found';
        }

        return view('../Views/Post/show.view.php', post: $post, content: $content);
    }

and my view show.view.php

<?php 

echo '<pre>';
print_r($post);
echo '</pre>';?>
<x-base title="Blog post">
    <?php dd($post); ?>
    <div class="max-w-4xl mx-auto px-4 py-8">
        <!-- Article header -->
        <header class="mb-8">
            <h1 class="text-7xl font-bold text-gray-900">{{ $post->title }}</h1>
            <div class="mt-4 text-sm text-gray-500">
                <span> by {{ $post->user->name }}</span>
                <span :if="$this->post->published_at"> on {{ $post->published_at->format('M j, Y') }}</span>
            </div>
            <!-- TLDR section as a quote -->
            <div :if="$post->tldr" class="mt-4">
                <blockquote class="border-l-4 border-gray-300 pl-4 italic text-gray-600">
                    <span class="font-black">TLDR: </span> <span class="italic">{{ $post->tldr }}</span>
                </blockquote>
            </div>
        </header>

        <!-- Article content -->
        <section class="prose prose-lg">
            {!! $content !!}
        </section>

    </div>

</x-base>

A dd($post);' in BlogController.php` returns this :

Image

The print_rand dd at the beginning of the view give that :

Image

The data in print_r and dd aren't the same.

Here it is my x-base template :

<html lang="en">

<head>
    <title :if="isset($title)">{{ $title }} | Cyclone</title>
    <title :else>Cyclone</title>

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <!-- Favicon -->
    <link rel="apple-touch-icon" sizes="180x180" href="/favicon/apple-touch-icon.png" />
    <link rel="icon" type="image/png" sizes="32x32" href="/favicon/favicon-32x32.png" />
    <link rel="icon" type="image/png" sizes="16x16" href="/favicon/favicon-16x16.png" />
    <link rel="manifest" href="/favicon/site.webmanifest" />

    <x-vite-tags />
    <script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js"></script>
    <x-slot name="styles" />


</head>

<body class="flex flex-col min-h-screen w-full">
    <div x-data="{
        scrolled: false,
        mobileMenuOpen: false,
        dropdowns: {
            about: false,
            portfolio: false,
            services: false,
            contact: false
        }
    }"
        x-init="window.addEventListener('scroll', () => { scrolled = window.scrollY > 10 })"
        class="relative">
        <x-menu />
        <!-- Overlay for when dropdown is active -->
        <div
            x-show="Object.values(dropdowns).some(val => val === true)"
            class="fixed inset-0 bg-black/20 backdrop-blur-sm z-30"
            @click="Object.keys(dropdowns).forEach(key => dropdowns[key] = false)">
        </div>
    </div>
    <main class="pt-16 flex-grow">
        <x-slot />
    </main>
    <x-footer />

</body>

</html>

A view of my sqlite database :

Image

And my model Post:

<?php

namespace App\models;

use App\Auth\User;
use Tempest\Database\IsDatabaseModel;

final class Post
{
    use IsDatabaseModel;

    public ?User $user = null;

    public string $title;

    public string $slug;

    public string $tldr;

    public string $markdown_file_path;

    public ?\DateTimeImmutable $created_at; 
    
    public ?\DateTimeImmutable $published_at; 

    public int $user_id;

    public function getContent(): string
    {
        return file_get_contents($this->markdown_file_path);
    }
}

If any question, let me know !!!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions