1+ <?php
2+ /**
3+ * Example: Dynamic Cache Control in MVC Views
4+ *
5+ * This example demonstrates how to dynamically enable or disable
6+ * caching for individual page renders in controller methods.
7+ */
8+
9+ use Neuron \Mvc \Controllers \Base ;
10+ use Neuron \Mvc \Responses \HttpResponseStatus ;
11+ use Neuron \Routing \Router ;
12+
13+ class BlogController extends Base
14+ {
15+ /**
16+ * Blog listing - can be cached
17+ */
18+ public function index ()
19+ {
20+ $ Posts = $ this ->getRecentPosts ();
21+
22+ // Enable caching for the blog listing page
23+ return $ this ->renderHtml (
24+ HttpResponseStatus::OK ,
25+ [ 'posts ' => $ Posts ],
26+ 'index ' ,
27+ 'blog ' ,
28+ true // Cache enabled
29+ );
30+ }
31+
32+ /**
33+ * User dashboard - should not be cached
34+ */
35+ public function dashboard ()
36+ {
37+ $ User = $ this ->getCurrentUser ();
38+ $ PersonalizedData = $ this ->getUserDashboardData ( $ User );
39+
40+ // Disable caching for user-specific content
41+ return $ this ->renderHtml (
42+ HttpResponseStatus::OK ,
43+ [
44+ 'user ' => $ User ,
45+ 'data ' => $ PersonalizedData
46+ ],
47+ 'dashboard ' ,
48+ 'default ' ,
49+ false // Cache explicitly disabled
50+ );
51+ }
52+
53+ /**
54+ * Static about page - force cache even if globally disabled
55+ */
56+ public function about ()
57+ {
58+ $ Content = $ this ->getStaticContent ( 'about ' );
59+
60+ // Force caching for static content
61+ return $ this ->renderHtml (
62+ HttpResponseStatus::OK ,
63+ [ 'content ' => $ Content ],
64+ 'about ' ,
65+ 'static ' ,
66+ true // Force cache even if globally disabled
67+ );
68+ }
69+
70+ /**
71+ * Search results - let global config decide
72+ */
73+ public function search ()
74+ {
75+ $ Query = $ _GET ['q ' ] ?? '' ;
76+ $ Results = $ this ->searchPosts ( $ Query );
77+
78+ // Use default cache behavior (null = use global config)
79+ return $ this ->renderHtml (
80+ HttpResponseStatus::OK ,
81+ [
82+ 'query ' => $ Query ,
83+ 'results ' => $ Results
84+ ],
85+ 'search ' ,
86+ 'default '
87+ // No cache parameter = uses global configuration
88+ );
89+ }
90+
91+ /**
92+ * Markdown documentation - cached
93+ */
94+ public function docs ()
95+ {
96+ $ Page = $ _GET ['page ' ] ?? 'index ' ;
97+
98+ // Enable caching for documentation pages
99+ return $ this ->renderMarkdown (
100+ HttpResponseStatus::OK ,
101+ [ 'page ' => $ Page ],
102+ $ Page ,
103+ 'docs ' ,
104+ true // Cache enabled for docs
105+ );
106+ }
107+
108+ // Mock methods for example
109+ private function getRecentPosts () { return []; }
110+ private function getCurrentUser () { return null ; }
111+ private function getUserDashboardData ( $ User ) { return []; }
112+ private function getStaticContent ( $ Page ) { return '' ; }
113+ private function searchPosts ( $ Query ) { return []; }
114+ }
115+
116+ /**
117+ * Usage Summary:
118+ *
119+ * 1. Pass `true` as the 5th parameter to force caching
120+ * 2. Pass `false` to disable caching for that specific render
121+ * 3. Pass `null` or omit the parameter to use global cache configuration
122+ *
123+ * This allows fine-grained control over caching on a per-page basis,
124+ * useful for mixing static and dynamic content in the same application.
125+ */
0 commit comments