# **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` --- ## **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 registerGate("blog", __DIR__ . "/apps/Blog.gate.php"); registerGate("shop", __DIR__ . "/apps/Shop.gate.php"); registerGate("admin", __DIR__ . "/apps/Admin.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\tools\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. Start your development server 2. Open browser to `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\tools\BasicGate; class Index extends BasicGate { /** * Default Page Event - triggered by 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 index-about.html */ public function page_event_about($evtp) { echo "About Page - Parameter: " . $evtp; } /** * Custom Page Event - triggered by index-contact.html */ public function page_event_contact($evtp) { echo "Contact Page"; } /** * Form Submit - triggered by index.html (POST) */ 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_view() { // $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 public function page_submit() { // This is called first on any form POST if ($this->frtMain->form->getRecID() == "") { // No record ID = Insert new record // page_insert() is automatically called after } else { // Has record ID = Update existing // page_update() is automatically called after } } // Called when inserting new record public function page_insert() { echo "Inserting new record..."; } // Called when updating existing record public function page_update() { echo "Updating record..."; } ``` ## **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*