givanz / Vvveb

Powerful and easy to use cms to build websites, blogs or ecommerce stores.
https://www.vvveb.com
GNU Affero General Public License v3.0
337 stars 75 forks source link

Controller->Action->View failed to pass data to template #154

Closed Shofiul-Alam closed 3 months ago

Shofiul-Alam commented 3 months ago

I reviewed your CMS system and I'm truly impressed by the innovative approach to caching and optimized performance. The adherence to MVC principles and separation of concerns is commendable. I particularly appreciate the simplicity of the CMS framework architecture and its minimal reliance on external libraries.

I am about to develop a new e-commerce solution for my company, and I am looking for a framework that integrates a page builder for our marketing team while also offering the flexibility to follow Domain-Driven Design patterns. Vvveb seems like the perfect framework for this purpose.

I have been studying the system code and core framework modules, and I am especially impressed by the powerful yet simple Vtpl template engine.

My project requires a significant amount of customization, including developing a 3D product configurator, enabling online quote requests, facilitating online booking for showroom visits, allowing customers to create mood boards, and most importantly, creating a REST API pipeline between our custom-developed ERP system and the new e-commerce solution.

During my research, I noticed that components such as app/component/post.php are bypassing any custom data passed from the action, relying solely on data retrieved from the database that was cached initially. This prevents me from passing any custom data outside the database model to the template when using placeholder attributes. Below is an example where I rewrote the request() method for the component:

`//called on each request function request(&$results, $index = 0) { $request = Request::getInstance(); $created_at = $request->get['created_at'] ?? ''; //revision preview

    if ($created_at && $results['post_id']) {
        //check if admin user to allow revision preview
        $admin = Admin::current();

        if ($admin) {
            $revisions = model('post_content_revision');
            $revision  = $revisions->get(['created_at' => $created_at, 'post_id' => $results['post_id'], 'language_id' => $results['language_id']]);

            if ($revision && isset($revision['content'])) {
                $results['content']    = $revision['content'];
                $results['created_at'] = $revision['created_at'];
            }
        }
    }

    **_$view = View::getInstance();
    if(isset($view->post) && is_array($view->post)){
        if(is_array($results)){
            $results = array_merge($results, $view->post);
        }else{
            $results = $view->post;
        }
    }_**

    return $results;
}`

This is also required for the results() method. I suggest improving it by providing View::instance() and component data through the BaseComponent class. Use properties and construction to make it available and include it in the result.

givanz commented 3 months ago

Thank you.

REST Api is planned and it will be added in a future release.

I changed ComponentBase to include View,Request and Session instances https://github.com/givanz/Vvveb/commit/d10f3bf87ef107bd50b7bfa8b00dbb1fe8fdc76d.

You can use $this->view inside component methods without having to use $view = View::getInstance();

$this->options can be used in any method like results or request to access component options set in the template

to access global options from ComponentBase like current site_id you can use self::$global['site_id']

You can also return false in cacheKey method to disable cache and have results called on each request where you can return data from view that was set from the controller.

class Post extends ComponentBase {

    function cacheKey() {
        //disable caching
        return false;
    }

    //called on each request
    function results() {
    }
}
Shofiul-Alam commented 3 months ago

@givanz Thank you so much. This change will definitely help. I really appreciate your prompt action.