SilverStripe 404 Fix

User
Sliver stripe custom page gives 404 error
Assistant
If your **custom page in SilverStripe** is returning a **404 error**, it typically means SilverStripe can't find a matching route or controller. Here's a checklist to debug and fix it:

### ✅ **1. Custom Page Type: Ensure it’s properly defined**

**Custom Page Class: `MyPage.php`**

<?php

use SilverStripeCMSModelSiteTree;

class MyPage extends SiteTree
{
    private static $db = [
        'CustomField' => 'Varchar'
    ];
}

**Custom Page Controller: `MyPageController.php`**

<?php

use PageController;

class MyPageController extends PageController
{
}

Ensure both files are in the correct directory, e.g., `app/src/`.

### ✅ **2. Add a Page in CMS**

You **must create a page of type `MyPage`** in the CMS admin:
– Go to `Pages` in the admin.
– Click “Add page”.
– Choose the correct type (`MyPage`).
– Publish the page.

If you haven’t added it in the CMS, the URL will not resolve and 404.

### ✅ **3. Run Dev Build**

After creating the page type, run:

/vendor/bin/sake dev/build flush=1

Or access in browser:

https://yoursite.com/dev/build?flush=1

### ✅ **4. Check Page URL Segment**

After creating your custom page (e.g., `My Custom Page`), SilverStripe assigns it a URL segment like:

https://yoursite.com/my-custom-page

Trying to visit the wrong or missing URL will give a 404.

### ✅ **5. Template Check (Optional)**

Make sure `MyPage.ss` template exists in:

themes/your-theme/templates/Layout/MyPage.ss

Fallback is `Page.ss`, but if nothing renders, you may get unexpected output.

### ✅ **6. Cache Issues**

Always use:

?flush=1

after making changes, especially templates or controllers.

### ✅ **7. Routing Conflict**

If you're using **custom routes in `routes.yml`**, make sure they don't conflict with page URLs.

### ✅ **8. Check Published Status**

If the page is not published or is under a draft, you’ll get 404 when logged out.

If none of these help, share:
– The custom page class name.
– Controller file content.
– How you’re accessing the URL.
– Whether the page is created in the CMS.

I'll help debug precisely.

User
Assistant
Thanks for sharing your `LandsPageController` code. From your example, you're expecting the `test()` method to work on a custom route like `/lands/test`, but you're getting a **404**. Let's debug it step-by-step based on SilverStripe conventions:

### ✅ Checklist to Fix 404 for Custom Controller Action

#### 1. ✅ **Check Matching Page Type Exists**

You must have a corresponding page type class like this:

namespace {
    use SilverStripeCMSModelSiteTree;

    class LandsPage extends SiteTree {
        // Define any fields or logic if needed
    }
}

#### 2. ✅ **Page Must Exist in CMS**

– Go to `/admin/pages`
– Add a new page of type **LandsPage**
– Publish it
– This will be available at something like:
`https://yoursite.com/lands`
(depending on what you name it)

#### 3. ✅ **Accessing the Custom Action**

Assuming you created the page with URL segment `/lands`, your custom method should be accessible via:

https://yoursite.com/lands/test

And it should return:

page test

### ❗ Problems in Your Code

#### A. **Namespace usage is incorrect or incomplete**

You're using:

namespace {
    use SilverStripeLessions;

– There's no class called `SilverStripeLessions` – it might be a typo (`Lessons`?).
– You usually don’t need to open `namespace {}` unless you're avoiding namespacing (which is discouraged in modern SilverStripe).

🔧 **Fix it like this**:

**LandsPage.php**

<?php
namespace AppPages;

use SilverStripeCMSModelSiteTree;

class LandsPage extends SiteTree {
    // your fields
}

**LandsPageController.php**

<?php
namespace AppPages;

use PageController;

class LandsPageController extends PageController {
    private static $allowed_actions = ['test'];

    public function test() {
        return "page test"; // or use return array to render a template
    }
}

**Then update config to autoload namespace**, or keep them in `app/` without namespace but NOT inside `namespace {}`.

### ✅ Rebuild After Changes

Every time you add or rename a page class or controller:

– Run in browser:

https://yoursite.com/dev/build?flush=all

### 🔁 Summary

| Task | Must Be

User
namespace {
use SilverStripeCMSModelSiteTree;

class LandsPage extends SiteTree {
// Define any fields or logic if needed
}
}
where I will create this

Assistant
Great — you’re on the right track. Here's exactly **where and how to create the `LandsPage` class** so that SilverStripe recognizes it:

### ✅ Where to Create `LandsPage.php`

Create the file in your project under:

/app/src/LandsPage.php

Or, if your `app/` folder has no `src/`, simply put it here:

/app/LandsPage.php

### ✅ Full Working Example

**File: `/app/LandsPage.php`**

<?php
namespace {
    use SilverStripeCMSModelSiteTree;

    class LandsPage extends SiteTree {
        // You can define fields and methods here
    }
}

**File: `/app/LandsPageController.php`**

<?php
namespace {
    use PageController;

    class LandsPageController extends PageController {
        private static $allowed_actions = ['test'];

        public function test() {
            return "page test";
        }
    }
}

### ✅ After Creating These Files

1. Run dev/build:

https://yoursite.com/dev/build?flush=1

2. Go to CMS admin:

https://yoursite.com/admin/pages

3. Add a new page → select type **“Lands Page”**.

4. Give it a URL segment like `lands`.

5. Publish the page.

### ✅ Now test your custom route:

Go to:

https://yoursite.com/lands/test

You should see:

page test

Let me know if you have custom folders (like `mysite/` instead of `app/`) or use Composer autoloading — I’ll tailor the structure.

Leave a Comment

Your email address will not be published. Required fields are marked *