Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Silverstripe Controller & Action


Go to End


4 Posts   2094 Views

Avatar
Naveed

Community Member, 10 Posts

18 April 2016 at 12:59pm

Edited: 18/04/2016 3:49pm

Hi Experts,

I would recommend the experts to upgrade the documentation to the level that makes some sense to the experienced developers but new commers to silverstripe. I found the silverstripe to learn hard due to its unstructured documentation.
I am stuck it on a very simple issue and didn't find any article in the documentation.
Problem:
I created a new controller in the mysite\code\controllers\Gallery.php
I created the following actions i.e.

<?php

class GalleryController extends Controller {
    private static $allowed_actions = array(
        'index',
        'createimage',
        'editimage',
        'deleteimage',
    );
    
    public function index(SS_HTTPRequest $request)
    {
        $res = "here in the index action of gallery controller <br />";
        echo $res;
        return $this->getResponse();
    }
    
    public function createimage(SS_HTTPRequest $request){
        $res = "here in the create action of gallery controller <br />";
        echo $res;
        return $this->getResponse();
    }
    
    public function editimage(SS_HTTPRequest $request)
    {
        echo "her in the edit action of gallery controller with picture id = $id <br />";
        return $this->getResponse();
    }
    
    public function deleteimage(SS_HTTPRequest $request)
    {
        echo "her in the delete action of gallery controller with picture id = $id <br />";
        //return $this->getResponse();
    } 
}

I am successfully access the default index action but not the others and is receiving the following error i.e.

Accessing the url i.e. http://localhost:8011/ss_pac/gallery/createimage
Error: Action 'createimage' isn't available on this handler.

I am getting the same error with editimage, deleteimage actions as well.

I am following this page in the documentation i.e. https://docs.silverstripe.org/en/3.3/developer_guides/controllers/introduction/

Here is my routes file at mysite\_config\routes.yml

## YAML Template.
# ---
Name: mysiteroutes
After: framework/routes#coreroutes
# ---
Director:
    rules:
        'gallery//$Action/$Id': 'GalleryController'

Looking for someone who will tell me how to reach to the particular action. Looking forward.

Cheers,
Naveed.

Avatar
Admonish

Community Member, 3 Posts

18 April 2016 at 2:35pm

Edited: 18/04/2016 2:37pm

Hello there Naveed. First off I want to apologize for the silverstripe documentation (or lack of it) in some areas. The silverstripe team are very busy as of late, due to the immense amount of work needed to finish out SS version 4.0. Now as for your question, I will say two things. Number 1: I highly encourage you to take a look at the Silverstripe video tutorials located here, specifically this video on controller actions. Number 2: Though I am not sure what the problem is (and have not worked with controller actions myself) just looking at your code I would guess that the

'createaction',
in your $allowed_actions array might have to be changed to:
'create',
Please know that this is only a guess and could very easily be wrong.

Hope that helps,
Admonish

P.S. As I learn SS more and more I am constantly referring to the api docs as they are SS ultimate source of truth

Avatar
Naveed

Community Member, 10 Posts

18 April 2016 at 3:22pm

Hi Admonish,

Thanks for your reply. You are right but the issue is the learning curve for Silverstripe is too hard due to lack of its structured documentation as I am struggling to understand it since last 2-weeks but it is pretty hectic.

I can use whatever valid "Action" name in the array as defined in the article mentioned in my post below. But silverstripe didn't work as required. I always prefer to read than to see video therefore is trying to cracking the documentation.

I have tried to changed the name of actions but in vain. In addition, not much examples / tutorials available for it over the internet especially for beginners.

Thanks once again for your answers and hope the silverstripe team will work on the documentation part soon and make it better for learning.

Cheers,
Naveed.

Avatar
Naveed

Community Member, 10 Posts

18 April 2016 at 3:47pm

Everything is written above was fine but here is the key point to note i.e.

After defining a new controller and its actions, silverstripe needs to flush it's cache i.e.

http://localhost:8011/ss_pac/gallery/createimage?flush=1

In this way, the new controller and it's action will working fine.

Enjoy and good luck to the new commers.
Naveed.