# Website Flow Guide

This file explains how the public website works in this project so we can rebuild the same mechanism in another Laravel project.

## 1. High-level architecture

This project is a Laravel 10 application.

For the public website, the request flow is mostly:

`browser request -> route -> controller method -> Eloquent model query -> optional resource transformation -> Blade view -> HTML response`

Main public website pieces:

- Routes: `routes/web.php`
- Main public controller: `app/Http/Controllers/Site/HomeController.php`
- Extra website helper controller: `app/Http/Controllers/MainController.php`
- Public views: `resources/views/web_site/...`
- Public models: mostly `app/Models/Site/...`, plus `app/Models/Category.php` and `app/Models/Service.php`
- Form requests: `app/Http/Requests/Site/...`
- Shared helper functions: `app/Helpers/main_helper.php`
- Base controller traits: `app/Traits/MainFunction.php`, `app/Traits/ImageProcessing.php`

## 2. Route loading

Laravel loads the route files from:

- `app/Providers/RouteServiceProvider.php`

Important part:

- `routes/web.php` is loaded with `web` middleware
- `routes/admin.php` is also loaded with `web` middleware
- `routes/api.php` is loaded with `api` middleware and `/api` prefix

So the public website pages are mainly in `routes/web.php`.

## 3. Public website route map

These are the main public routes in `routes/web.php`:

| URL | Method | Controller method | Purpose | View |
| --- | --- | --- | --- | --- |
| `/` | GET | `HomeController@index` | Home page | `web_site.home` |
| `/projects` | GET | `HomeController@projects` | Projects list | `web_site.projects.projects` |
| `/projects/{id}` | GET | `HomeController@one_project` | Project details | `web_site.projects.project_detail` |
| `/blogs` | GET | `HomeController@blogs` | Blogs list | `web_site.blogs.blog` |
| `/blogs/{id}` | GET | `HomeController@one_blog` | Blog details | `web_site.blogs.blog_detail` |
| `/blogsComment` | POST | `HomeController@blogsComment` | Blog comments | not confirmed in current controller |
| `/contact_us` | GET | `HomeController@contact_us` | Contact page | `web_site.contact` |
| `/SaveContact_us` | POST | `HomeController@SaveContact_us` | Save contact form | redirect back |
| `/StudentRegistration` | GET | `HomeController@StudentRegistration` | Student registration page | expected website view |
| `/SaveStudentRegistration` | POST | `HomeController@SaveStudentRegistration` | Save student registration | not found in current controller |
| `/events` | GET | `HomeController@events` | Events list | `web_site.events.events` |
| `/events/{id}` | GET | `HomeController@one_events` | Event details | `web_site.events.eventDetails` |
| `/eventRegistration/{id}` | GET | `HomeController@eventRegistration` | Event registration page | `web_site.events.eventRegistration` |
| `/SaveEventRegistration` | POST | `HomeController@saveEventRegistration` | Save event registration | redirect back |
| `/videos` | GET | `HomeController@video` | Videos page | `web_site.videos` |
| `/photos` | GET | `HomeController@photos` | Gallery list | `web_site.gallery.gallery` |
| `/photosDetails/{id}` | GET | `HomeController@photosDetails` | Gallery details | `web_site.gallery.gallery_details` |
| `/teachers` | GET | `HomeController@teacher` | Teachers page | `web_site.teacher` |
| `/about_us` | GET | `HomeController@about` | About page | `web_site.about` |
| `/our_story` | GET | `HomeController@story` | Story page | `web_site.our_story` |
| `/our_approach` | GET | `HomeController@approach` | Approach page | `web_site.our_approach` |
| `/our_history` | GET | `HomeController@history` | History page | `web_site.our_history` |
| `/services/{id}` | GET | `HomeController@services` | Services by category | `web_site.services.services` |
| `/services/Details/{id}` | GET | `HomeController@one_service` | Service details | `web_site.services.details` |
| `/case_Studies` | GET | `HomeController@case_Studies` | Case studies list | `web_site.Case_Studies.all` |
| `/case_Studies/{id}` | GET | `HomeController@one_case_Studies` | Case study details | `web_site.Case_Studies.detail` |
| `/subscribe` | POST | `HomeController@subscribe` | Newsletter subscribe | JSON response |
| `/subcategory` | GET | `HomeController@subcategory` | Service categories helper page | `web_site.services.catservices` |
| `/getBlogs` | GET | `MainController@getBlogs` | Blog search/filter result | `web_site.blogs.blog` |

## 4. Real page flow examples

### Home page

Files involved:

- Route: `routes/web.php`
- Controller: `HomeController@index`
- Models used:
  - `App\Models\Site\SiteBlog`
  - `App\Models\Site\SiteEvent`
  - `App\Models\Service`
  - `App\Models\Site\SiteCase_Studies`
  - `App\Models\Site\SiteFeedback`
  - `App\Models\Site\SitePartners`
  - `App\Models\Site\statistics`
  - `App\Models\Category`
  - `App\Models\Site\About`
  - `App\Models\Site\Trainee_Review`
  - `App\Models\Site\TrainingCourses`
  - `App\Models\Site\WeDo`
- View: `resources/views/web_site/home.blade.php`

Flow:

1. Browser opens `/`
2. Route calls `HomeController@index`
3. Controller queries many models and builds `$data`
4. Controller returns `view('web_site.home', $data)`
5. Blade view renders sections based on the passed variables
6. Layout comes from `resources/views/web_site/layouts/master.blade.php`

### Blog list page

Files involved:

- Route: `/blogs`
- Controller: `HomeController@blogs`
- Model: `App\Models\Site\SiteBlog`
- View: `resources/views/web_site/blogs/blog.blade.php`

Flow:

1. Route calls `blogs()`
2. Controller gets paginated blogs ordered by date
3. Controller returns the Blade list view
4. View loops over `$all`
5. Pagination links are rendered in Blade

### Blog details page

Files involved:

- Route: `/blogs/{id}`
- Controller: `HomeController@one_blog`
- Model: `App\Models\Site\SiteBlog`
- Resource: `App\Http\Resources\Site\BlogResource`
- View: `resources/views/web_site/blogs/blog_detail.blade.php`

Flow:

1. Route sends `{id}` to controller
2. Controller loads one blog with `findOrFail($id)`
3. Blog is transformed using `new BlogResource($one_data)`
4. Result is converted into a plain object by `$this->prepare_data(...)`
5. Controller loads recent blogs for sidebar
6. Controller returns the detail Blade view
7. View reads resource-style fields like `blogTitle`, `blogDetails`, `blogDate`, `blogImgaes`

### Contact form submit

Files involved:

- Route: `/SaveContact_us`
- Controller: `HomeController@SaveContact_us`
- Form request: `App\Http\Requests\Site\ContactRequest`
- Model: `App\Models\Site\SiteContact`
- View after redirect: `resources/views/web_site/contact.blade.php`

Flow:

1. User submits contact form
2. Laravel resolves `ContactRequest`
3. Validation rules run before controller logic
4. If valid, controller uses `SiteContact::create($request->all())`
5. Success message is shown with toastr
6. User is redirected back to `contact_us`

### Newsletter subscribe

Files involved:

- Route: `/subscribe`
- Controller: `HomeController@subscribe`
- Model: `App\Models\Site\Newsletter`

Flow:

1. Frontend sends POST request
2. Controller validates the email inline
3. Newsletter record is created
4. JSON success response is returned

This route is different from most website pages because it returns JSON instead of a Blade view.

## 5. Current controller pattern

The public site is mostly controlled by:

- `app/Http/Controllers/Site/HomeController.php`

Current pattern inside this controller:

- Read route parameter or request input
- Query Eloquent models directly inside controller
- Sometimes eager-load relations with `with(...)`
- Sometimes paginate results
- Sometimes transform detail records with API Resources
- Return a Blade view with compacted variables

This means the project does **not** currently use a dedicated service layer for public website pages. The controller itself contains most of the website page orchestration.

## 6. Models and relationships used by the website

### Main website models

Frequently used models:

- `app/Models/Site/SiteBlog.php`
- `app/Models/Site/SiteEvent.php`
- `app/Models/Site/SiteProjects.php`
- `app/Models/Site/SitePhoto.php`
- `app/Models/Site/SiteCase_Studies.php`
- `app/Models/Site/SitePartners.php`
- `app/Models/Site/SiteFeedback.php`
- `app/Models/Site/SiteStaff.php`
- `app/Models/Site/SiteVideo.php`
- `app/Models/Site/SiteContact.php`
- `app/Models/Site/SiteEventRegister.php`
- `app/Models/Site/Newsletter.php`
- `app/Models/Category.php`
- `app/Models/Service.php`

### Important model behavior

#### `Category`

`app/Models/Category.php`

Important details:

- Uses `Kalnoy\Nestedset\NodeTrait`
- Supports parent/child tree structure
- Has `services()` relation
- Has translated fields with `spatie/laravel-translatable`

This is why website service pages can fetch:

- root categories
- descendants of a category
- services inside category trees

#### `Service`

`app/Models/Service.php`

Important details:

- Has `images()` relation
- Belongs to `category()`
- Adds computed attributes like `image_url` and `file_url`
- Uses translated fields

#### `SiteBlog` and `SiteEvent`

Important details:

- Each model computes `sort_date` during `saving()`
- Each model has image accessors for frontend use
- Both are used heavily for ordered website listings

## 7. Request validation pattern

Form Requests live under:

- `app/Http/Requests/Site/...`

Examples:

- `app/Http/Requests/Site/ContactRequest.php`
- `app/Http/Requests/Site/StudentRegistrationRequest.php`

Mechanism:

1. Route points to controller method
2. Controller method type-hints a custom `FormRequest`
3. Laravel automatically validates before entering the method
4. If validation fails, user is redirected back with errors
5. If validation passes, controller continues normally

This is the correct pattern to copy into the other project for form submissions.

## 8. Resource transformation pattern

Some detail pages use Laravel API Resources even though the final output is a Blade view.

Examples imported in `HomeController`:

- `BlogResource`
- `EventResource`
- `PhotoResource`
- `ProjectResource`

Current mechanism:

1. Load model with Eloquent
2. Wrap it with a Resource class
3. Convert resource to plain data using `prepare_data()`

`prepare_data()` comes from:

- `app/Traits/MainFunction.php`

It does:

- `json_encode($data)`
- `json_decode($json)`

So the Blade view receives a plain object instead of the original Eloquent model or `JsonResource` instance.

This is a project-specific pattern. If you rebuild in another project, you can:

- keep the same pattern for compatibility, or
- simplify by passing Eloquent models directly to views, or
- move fully to Resources only for APIs

## 9. View structure

Public views are under:

- `resources/views/web_site`

Layout structure:

- `layouts/master.blade.php`
- `layouts/head.blade.php`
- `layouts/main-headerbar.blade.php`
- `layouts/footer.blade.php`
- `layouts/footer-scripts.blade.php`

Normal page structure:

1. Page Blade file uses `@extends('web_site.layouts.master')`
2. Page content goes inside `@section('content')`
3. Master layout includes header and footer partials
4. Shared website data is also pulled from helper functions inside the layout

### Shared helper usage in views

The website relies on helper functions from:

- `app/Helpers/main_helper.php`

Examples used in views:

- `getMainData()`
- `getDefultImage()`
- `getDefultText()`
- `formatDateDayDisplay()`

These helpers are auto-loaded through `composer.json`.

If you copy this mechanism to another project, also copy:

- helper file
- composer autoload files entry
- any helper dependencies

## 10. Special website conventions in this project

### A. Layout-level shared data

`resources/views/web_site/layouts/master.blade.php` calls:

- `getMainData()`

That means some site-wide info is loaded directly in Blade, not injected from every controller method.

### B. Mixed rendering style

The project mixes:

- direct Eloquent model passing
- `compact(...)`
- arrays like `$data`
- Resource transformation for detail pages
- inline request validation for some endpoints
- dedicated Form Requests for others

So if you reproduce this exactly, keep the same flexible style. If you want a cleaner rebuild, standardize it.

### C. Pagination pattern

Many listing pages do:

- `paginate(...)`
- `with('i', ($request->input('page', 1) - 1) * 12)`

This is used in views for row numbering or page offsets.

## 11. Things to copy to another project

If the goal is to build another project with the same website mechanism, copy these layers in this order:

### Step 1: Route structure

Create a public route file similar to `routes/web.php` and map each page to a controller method.

### Step 2: Controller structure

Create one main website controller like `HomeController` that:

- handles public pages
- loads page-specific data
- returns Blade views

### Step 3: Models

Copy or rebuild the needed models with:

- fillable fields
- relationships
- appended accessors
- translatable fields
- nested category tree behavior if services/categories are needed

### Step 4: Validation

Create Form Request classes for POST forms such as:

- contact form
- registration forms
- subscription forms if needed

### Step 5: Resources

If you want the same detail-page data shape, also copy:

- Resource classes in `app/Http/Resources/Site`
- `prepare_data()` helper behavior

### Step 6: Views

Copy:

- `resources/views/web_site/layouts/*`
- each page view
- partials used inside them

### Step 7: Helpers

Copy:

- `app/Helpers/main_helper.php`
- composer autoload entry for helper files

### Step 8: Frontend assets

The views depend on many CSS, JS, and image assets inside:

- `public/assets_web`
- and some `public/assets`

Without these assets, copied views may render incorrectly.

## 12. Recommended folder structure for the new project

If you want the same mechanism, keep this shape:

```text
app/
  Http/
    Controllers/
      Site/
        HomeController.php
      MainController.php
    Requests/
      Site/
        ContactRequest.php
        ...
    Resources/
      Site/
        BlogResource.php
        EventResource.php
        ...
  Models/
    Site/
      SiteBlog.php
      SiteEvent.php
      ...
    Category.php
    Service.php
  Helpers/
    main_helper.php
  Traits/
    MainFunction.php
resources/
  views/
    web_site/
      layouts/
      blogs/
      events/
      gallery/
      projects/
      services/
routes/
  web.php
public/
  assets_web/
```

## 13. Practical migration recipe

To recreate a single page in another project:

1. Copy the route entry
2. Copy the controller method
3. Copy the model and its relations/accessors
4. Copy any Resource used by that method
5. Copy the Blade view
6. Copy any helper functions the view calls
7. Copy required assets and translation keys
8. Test the page end-to-end

Example for blogs module:

1. Copy blog routes from `routes/web.php`
2. Copy `blogs()` and `one_blog()` methods
3. Copy `SiteBlog`, related image/file models, and `BlogResource`
4. Copy `resources/views/web_site/blogs/*`
5. Copy helper functions and translations used in blog views

## 14. Important issues found in the current codebase

These should be noted before cloning the mechanism into another project:

- `routes/web.php` contains routes for `SaveStudentRegistration` and `StudentRegistration`, but the matching methods were not found in the current `HomeController.php` file I reviewed.
- `routes/web.php` references `blogsComment`, but that method was also not present in the reviewed `HomeController.php`.
- `contact_us` and `our_history` appear more than once in `routes/web.php`.
- `saveEventRegistration()` redirects to `route('eventRegistration')`, but that route requires an `{id}` parameter, so this may break unless handled elsewhere.
- `MainFunction` trait contains constructors and references to models/namespaces that look unrelated to this website, so copy it carefully.

Because of this, for the new project it is better to copy the **working pattern** rather than blindly duplicating every file.

## 15. Best way to rebuild this cleanly

If we rebuild this in another project, I recommend keeping the same external behavior but using this cleaner rule set:

- Use one public website controller per module or one main `HomeController` if the site is small
- Use Form Requests for every POST form
- Use Eloquent models directly in views unless a Resource transformation is really needed
- Keep shared site data in a View Composer or middleware instead of calling helpers from Blade when possible
- Keep route names identical if frontend links depend on them
- Keep the same view folder structure so moving templates is easier

## 16. Minimum set to send me next time

If you want me to generate the same documentation for another project or compare mechanisms between two projects, send me:

- the route file
- the main website controller
- the related models
- the request classes
- the Blade views
- helper files if the views call global functions

Then I can produce the same kind of architecture file very quickly.

