Menu
Theme
Bachelor of Science in Computer Science
Course Content

PHP/Laravel

Web Development

Jenga Tovuti Kama Pro: Your First Steps into PHP & Laravel!

Habari student! Welcome to the next exciting chapter in your web development journey. You've learned how to build the 'body' of a website with HTML and how to dress it up with CSS. Now, it's time to give your website a 'brain'! We're going to learn about PHP and its most popular framework, Laravel. Think of the websites you use every day, like Jumia, eCitizen, or even your school's portal. They aren't just static pages; they are smart, they remember you, and they do things! That 'smartness' is what we are about to build.

Image Suggestion: [A vibrant, optimistic digital illustration of a young Kenyan student sitting at a desk with a laptop. The screen shows lines of code. Outside the window is a view of a modern Nairobi skyline. The style should be colourful and inspiring, with a mix of tech and local culture.]

Part 1: What is PHP? The Engine Room of the Web

Imagine you're at Java House and you want to order a coffee. You don't go into the kitchen yourself, right? You tell the waiter what you want, the waiter takes the order to the kitchen, the chef prepares it, and the waiter brings it back to you. The web works in a similar way!

  • You (The Browser): You request a webpage by typing a URL.
  • The Waiter (The Web Server): It receives your request.
  • The Kitchen (The Server's Backend): This is where the magic happens!
  • The Chef (PHP): PHP is a server-side programming language. It works in the kitchen, not at the customer's table. It gets instructions, processes information (like checking a database), and prepares the final HTML page.

So, PHP is the chef that builds the webpage on the server before it's sent to your browser. It can talk to databases, handle form submissions, and much more. Here's what "Hello, World!" looks like in PHP:

<?php
  echo "Habari Dunia!"; // 'echo' is how we print things in PHP
?>
Real-World Scenario: M-Pesa Confirmation

When you send money via M-Pesa, your phone sends a request. A server, likely running a language like PHP, receives it. The PHP script then does the following:
1. Checks if your balance is sufficient.
2. Deducts the amount from your account in the database.
3. Adds the amount to the recipient's account.
4. Generates the confirmation messages for both of you.
All this happens on the server, in the "kitchen"!

Part 2: What is Laravel? The Ultimate Fundi's Toolbox

So if PHP is the chef, why do we need Laravel? Think about building a house. You could make your own bricks from mud, chop your own timber, and mix your own cement. That's like writing with Plain PHP. It works, but it's slow, difficult, and you might make many mistakes.

A framework like Laravel is like going to a modern hardware store. They give you a strong blueprint (architecture), perfectly made bricks (pre-built components), secure doors and windows (security features), and ready-to-install plumbing (functions for common tasks). You still build the house, but you do it faster, safer, and in a more organised way. Laravel is a PHP framework that gives us a fantastic structure and amazing tools to build powerful web applications quickly.

Image Suggestion: [A split-panel anime-style drawing. On the left, a frustrated person is trying to build a small, wobbly shack from scratch with raw mud and sticks, labeled 'Plain PHP'. On the right, a confident person is easily assembling a sleek, modern house using a high-tech kit with pre-made panels and tools, labeled 'Laravel'.]

Laravel uses an architecture called MVC (Model-View-Controller). It’s a way to keep your code organised. Sawa?


        User Request
              │
              ▼
      ┌───────────────┐
      │    Router     │ (The Boda Boda rider who knows all the routes)
      └───────────────┘
              │
              ▼
      ┌───────────────┐
      │  Controller   │ (The Shop Manager - handles logic)
      └───────────────┘
       │             │
       ▼             ▼
┌───────────────┐  ┌───────────────┐
│     Model     │  │      View     │
│ (The Store-   │  │ (The Shop     │
│ keeper - gets │  │ Display - what│
│ data from DB) │  │ the user sees)│
└───────────────┘  └───────────────┘
  • Model: Manages your data. It talks directly to your database. Think of it as the storekeeper who knows where every item is.
  • View: The part the user sees. It’s the HTML and CSS. It's the beautiful display at the front of the shop.
  • Controller: The brain of the operation. It takes requests from the user, asks the Model for data, and tells the View what to display. It's the shop manager.

Part 3: Twende Kazi! Your First Laravel Steps

To start, you need a tool called Composer. Think of Composer as your "mjengo foreman" who gets all the right tools and materials (packages) for your project. Once you have it installed, you can create a new Laravel project with one command:

composer create-project laravel/laravel my_school_project

This command tells Composer to build a new Laravel project in a folder called my_school_project.

Creating Your First Route

A route is like a signpost. It tells your application what to do when a user visits a specific URL. Routes are defined in the routes/web.php file.

Let's create a route for a page that lists the counties in Kenya.

<?php

use Illuminate\Support\Facades\Route;

// When someone visits your homepage ('/'), show the welcome view.
Route::get('/', function () {
    return view('welcome');
});

// Our new route! When someone visits '/counties', do this...
Route::get('/counties', function () {
    return 'Here is a list of all 47 counties!';
});

Simple, right? The URL /counties is now connected to that piece of code.

Part 4: A Practical Calculation - School Fee Balance

Let's see how the Controller can handle some logic. Imagine we have a form where a parent enters the total fees and the amount they have paid. We need to calculate the balance.

Here’s a simple formula we can use:

Fee Balance = Total School Fees - Amount Paid

In a Controller (e.g., FeeController.php), the code might look something like this. Don't worry about understanding every single line yet, just focus on the logic!

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request; // This handles the incoming form data

class FeeController extends Controller
{
    // This function is named 'calculate'
    public function calculate(Request $request)
    {
        // 1. Get the data from the form
        $total_fees = $request->input('total_fees'); // e.g., 50000
        $amount_paid = $request->input('amount_paid'); // e.g., 20000

        // 2. Perform the calculation
        $balance = $total_fees - $amount_paid; // 50000 - 20000 = 30000

        // 3. Return the result to a view to show the user
        // The view will receive a variable named 'fee_balance' with the value 30000
        return view('fee_result', ['fee_balance' => $balance]);
    }
}

See? The Controller did the math! It took the input, performed a calculation, and is now ready to send the result to a View file called fee_result.blade.php to be displayed nicely for the parent.

Conclusion: Your Journey Has Just Begun!

Wow, that was a lot, but you made it! Let's do a quick recap:

  • PHP is the powerful engine that works on the server to build dynamic websites.
  • Laravel is an amazing framework (toolbox) that makes building with PHP faster, safer, and more organised.
  • MVC (Model, View, Controller) is the blueprint Laravel uses to keep code neat.

You have taken your first step into the world of backend development. This is how real-world, powerful applications are built. The next steps are learning how to connect to a database to save and retrieve information (using Laravel's Eloquent), create user login systems, and build complex forms.

Keep practicing, stay curious, and you'll be building the next great Kenyan web application in no time. Kazi nzuri!

Pro Tip

Take your own short notes while going through the topics.

Previous JavaScript
KenyaEdu
Add KenyaEdu to Home Screen
For offline access and faster experience