Documentation

On this page

Download full book (text)

# **Chapter 2: Registering Your First Gate - From URL to Page Event** > **File Extension Reference:** > - Gate Files: `*.gate.php` (e.g., `Index.gate.php`) > - Front Files: `*.front` (e.g., `index_main.front`) > - Master Files: `*.mast.php` (e.g., `default/master.mast.php`) > - Front Places: `*.place.front` or `*.place.php` > **Try it:** This chapter's flow is the registered gate `ex01flow` > (`apps/examples/Ex01flow.gate.php`, registered in `reg.php`). Open > `http://localhost/SartajPhpDoc/ex01flow.html` and > `http://localhost/SartajPhpDoc/ex01flow-hello-world.html`. --- ## **2.1 The Registration Process** Before your Gate can handle requests, you must register it in the `reg.php` file. This tells SartajPhp which Gate class to load for which URL. ### **The registerGate Function** ```php registerGate("gate_name", "path_to_gate_file.php"); ``` Parameters: - **gate_name** - The word used in URL (e.g., "index" from "index.html") - **path_to_gate_file.php** - Full path to your Gate file ### **Example: Registering the Index Gate** ```php <?php // reg.php - Application Registry File // Register the main Index Gate registerGate("index", __DIR__ . "/apps/Index.gate.php"); // Register additional Gates (real lines from this project's reg.php) registerGate("blog", __DIR__ . "/apps/examples/Blog.gate.php"); registerGate("product", __DIR__ . "/apps/examples/Product.gate.php"); registerGate("task", __DIR__ . "/apps/examples/Task.gate.php"); ``` ## **2.2 How Registration Maps to URLs** When you register `registerGate("index", ...)`, here's what happens: ``` Browser requests: index.html ↓ SartajPhp looks for registered Gate with name "index" ↓ Finds: registerGate("index", __DIR__ . "/apps/Index.gate.php") ↓ Loads: apps/Index.gate.php ↓ Creates: new Index() object ↓ Executes: page_new() method (default event) ``` ## **2.3 Creating Your First Gate Class** Now let's create the Gate class that was registered: ```php <?php // apps/Index.gate.php use Sphp\Gates\BasicGate; class Index extends BasicGate { /** * Lifecycle Event: onstart * Called once when the Gate is first created * Use for initialization */ public function onstart() { // Initialize your Gate here } /** * Page Event: page_new * Called when browser requests index.html * This is the DEFAULT event for any Gate */ public function page_new() { echo "Hello from Index Gate!"; } } ``` ## **2.4 Testing Your Gate** After registering and creating your Gate: 1. Make sure the project is served (this one runs under Apache as subfolder `SartajPhpDoc`) 2. Open browser to `http://localhost/SartajPhpDoc/index.html` 3. You should see "Hello from Index Gate!" The flow: ``` index.html → Gate: "index" → Event: "new" → Index::page_new() ``` ## **2.5 Handling Different Events** Let's extend our Gate to handle multiple events: ```php <?php // apps/Index.gate.php use Sphp\Gates\BasicGate; class Index extends BasicGate { /** * Default Page Event - triggered by URL index.html */ public function page_new() { // By default BasicGate enable Front File on New-Event, So Master File will be send // Stop FrontFile + MasterFile to send $this->showNotFrontFile(); echo "Home Page"; } /** * Custom Page Event - triggered by URL index-about.html */ public function page_event_about($evtp) { echo "About Page - Parameter: " . $evtp; } /** * Custom Page Event - triggered by URL index-contact.html */ public function page_event_contact($evtp) { echo "Contact Page"; } /** * Form Submit - triggered by URL index.html (POST) * This will not trigger if Form Submit on any other URL like index-form1_submit.html * So you can submit multiple Forms to different Gate's Events with change of URL */ public function page_submit() { echo "Form Submitted!"; } } ``` URL mapping: ``` index.html → Index::page_new() index-about.html → Index::page_event_about("") index-contact.html → Index::page_event_contact("") index.html (POST) → Index::page_submit() ``` ## **2.6 Event Parameter Handling** When your URL includes a parameter, it gets passed to your event method: ```php // URL: blog-view-5.html public function page_event_view($evtp) { // $evtp = "5" echo "Viewing record ID: " . $this->page->evtp; } // URL: user-profile-john.html public function page_event_profile($evtp) { // $evtp = "john" echo "User Profile: " . $evtp; } // URL: shop-search-laptop-price-100.html public function page_event_search($evtp) { // $evtp = "laptop-price-100" // You parse it yourself $parts = explode('-', $evtp); // $parts = ["laptop", "price", "100"] } ``` ## **2.7 Automatic Events** SartajPhp automatically triggers certain events based on form submissions: ```php // Form submission handling only Trigger, Form submit to landing URL like gate.html public function page_submit() { // This is called on form POST /** New Version of SartajPhp removed page_insert, page_update, page_view and page_delete events. * So we need to handle it manually. * Get the HTMLForm component from the FrontFile, then branch on isUpdateRequest(). */ $form2 = $this->frtMain->getComponent('form2'); if ($form2->isUpdateRequest()) { // Encrypted record token (form2re) present = Update existing $this->page->updateData($form2, $this->getExtraFields()); } else { // No record token = Insert new record $this->page->insertData($form2, $this->getExtraFields()); } } // Extra columns not present in the form private function getExtraFields() { $extra = array(); $extra[]['userid'] = 0; $extra[]['parentid'] = 0; $extra[]['submit_timestamp'] = time(); $extra[]['update_timestamp'] = time(); return $extra; } ``` > **Tested pattern:** `$form2->isUpdateRequest()` is true when the form carries the > hidden encrypted record token `form2re` (rendered automatically by `HTMLForm` for > edit requests). See `apps/examples/Blog.gate.php` `page_submit()` for the full > working version, and Chapter 4 for the complete flow. ## **2.8 Chapter Summary** Key points from this chapter: 1. **Register Gate** in `reg.php` using `registerGate("name", "path")` 2. **Create Gate class** extending `BasicGate` 3. **Page Events** are methods like `page_new()`, `page_submit()`, `page_event_name()` 4. **URL maps** directly: `gate-event-parameter.html` → `Gate::page_event_name(parameter)` 5. **Default event** is `page_new()` when no event specified ## **2.9 What's Next?** In the next chapter, you'll learn about Gate lifecycle events (`onstart`, `onready`, `onrun`, `onrender`) and when to use each one for initialization and processing. --- *Next: Chapter 3 - The Gate Lifecycle Events*