Jump to content

How to set content by a ID?


Michael

Recommended Posts

I guess that $this->get can do the job.

For example, for http://awesomecompany.com/client/plugin/my_plugin/page/12/ $page will be 12

if (!empty($this->get[0]) && is_numeric($this->get[0])) {
	$page = $this->get[0];
} else {
	$page = 1;
}

Then you can get the page from your model

Loader::loadModels($this, ['MyPlugin.MyModel']);

$page = $this->MyModel->getPage($page);

 

Link to comment
Share on other sites

3 minutes ago, cyandark said:

I guess that $this->get can do the job.

For example, for http://awesomecompany.com/client/plugin/my_plugin/page/12/ $page will be 12


if (!empty($this->get[0]) && is_numeric($this->get[0])) {
	$page = $this->get[0];
} else {
	$page = 1;
}

Then you can get the page from your model


Loader::loadModels($this, ['MyPlugin.MyModel']);

$page = $this->MyModel->getPage($page);

 

that sounds like what I did similar to that in my vultr module to grab the saved plan from the db to be selected by default in the dropdown select box on editing a package 

I would "fish" the code out I used to do that but I don't think it is relevant to what mike is trying to do since he wants to do exactly as you described @cyandark because mine was for a module this is probably in relation to mike's blestaforums plugin. 

Link to comment
Share on other sites

39 minutes ago, cyandark said:

I guess that $this->get can do the job.

For example, for http://awesomecompany.com/client/plugin/my_plugin/page/12/ $page will be 12


if (!empty($this->get[0]) && is_numeric($this->get[0])) {
	$page = $this->get[0];
} else {
	$page = 1;
}

Then you can get the page from your model


Loader::loadModels($this, ['MyPlugin.MyModel']);

$page = $this->MyModel->getPage($page);

 

Thanks mate,

So I put the top code in a model called: forums_functions.php

I put the bottom code in the controller: blesta_forums_controller.php in a function eg:

public function forum(){
        $forum_id = $this->ForumsFunctions->getForum($forum);
        echo $forum_id;
    }

and it just fails to load up do I need a pdt or something as the controller is forum so the id comes after so I was hoping we don't need a pdt as we'd need one for each id wouldn't we?

http://demo.blesta.store/client/plugin/blesta_forums/main/index/ if you click announcement or something they just error out.

Link to comment
Share on other sites

5 minutes ago, BlestaStore said:

Thanks mate,

So I put the top code in a model called: forums_functions.php

I put the bottom code in the controller: blesta_forums_controller.php in a function eg:


public function forum(){
        $forum_id = $this->ForumsFunctions->getForum($forum);
        echo $forum_id;
    }

and it just fails to load up do I need a pdt or something as the controller is forum so the id comes after so I was hoping we don't need a pdt as we'd need one for each id wouldn't we?

http://demo.blesta.store/client/plugin/blesta_forums/main/index/ if you click announcement or something they just error out.

seems to be 404 error to me, you might want to check your php error log to verify it isn't a php syntax error.

Link to comment
Share on other sites

Assuming you have a controller named "Forum" and a "page" function.

 http://awesomecompany.com/client/plugin/my_plugin/forum/page/1/

class Forum extends MyPluginController {

    public function index() {
        // Main view
    }

    public function page() {
        if (!empty($this->get[0]) && is_numeric($this->get[0])) {
            $page_id = $this->get[0];
        } else {
            $page_id = 1;
        }

        // Load forum model
        Loader::loadModels($this, ['MyPlugin.ForumsFunctions']);

        // Get forum page
        $forum = $this->ForumsFunctions->getForum($page_id);
    }
}

 

Link to comment
Share on other sites

37 minutes ago, cyandark said:

Assuming you have a controller named "Forum" and a "page" function.

 http://awesomecompany.com/client/plugin/my_plugin/forum/page/1/


class Forum extends MyPluginController {

    public function index() {
        // Main view
    }

    public function page() {
        if (!empty($this->get[0]) && is_numeric($this->get[0])) {
            $page_id = $this->get[0];
        } else {
            $page_id = 1;
        }

        // Load forum model
        Loader::loadModels($this, ['MyPlugin.ForumsFunctions']);

        // Get forum page
        $forum = $this->ForumsFunctions->getForum($page_id);
    }
}

 

So mate if I put the page() information in the index it would show /forum/1/ mate?

Link to comment
Share on other sites

13 minutes ago, BlestaStore said:

So mate if I put the page() information in the index it would show /forum/1/ mate?

that is sounding like you are looking for a way to rewrite the url so for example if announcements is forum id one it shows as {blestaroot}/plugin/blestaforums/forum/1/ correct? if so that sounds like some sort of route or rewrite you would have to tell blesta about.

I would recommend in your forum controller php file mike to put what cyandark showed in your index function as such:

class Forum extends MyPluginController {
.....
    public function index() {
        if (!empty($this->get[0]) && is_numeric($this->get[0])) {
            $page_id = $this->get[0];
        } else {
            $page_id = 1;
        }

        // Load forum model
        Loader::loadModels($this, ['MyPlugin.ForumsFunctions']);

        // Get forum page
        $forum = $this->ForumsFunctions->getForum($page_id);
    }
....
}

put the above in your index function for your forum controller and yes it will show up with the structure you want in the url.

eg this structure:

http://awesomecompany.com/client/plugin/my_plugin/forum/1/

that would get the forum id 1 as shown in the example url like you want.

do note the .... is to be your existing code don't copy the .'s please they have no real purpose other than being a placeholder for your other code. 

Link to comment
Share on other sites

19 minutes ago, BlestaStore said:

So mate if I put the page() information in the index it would show /forum/1/ mate?

Yes, but will be accessible from /forum/index/1/

If you want to delete the index part of the url you will need to make a route like this

Router::route("^forum/(.+)", "/my_plugin/forum/index/$1");

You can take as an example the main controller of a previous version of BlestaCMS. It works in a similar way.

Link to comment
Share on other sites

11 minutes ago, timnboys said:

that is sounding like you are looking for a way to rewrite the url so for example if announcements is forum id one it shows as {blestaroot}/plugin/blestaforums/forum/1/ correct? if so that sounds like some sort of route or rewrite you would have to tell blesta about.

I would recommend in your forum controller php file mike to put what cyandark showed in your index function as such:


class Forum extends MyPluginController {
.....
    public function index() {
        if (!empty($this->get[0]) && is_numeric($this->get[0])) {
            $page_id = $this->get[0];
        } else {
            $page_id = 1;
        }

        // Load forum model
        Loader::loadModels($this, ['MyPlugin.ForumsFunctions']);

        // Get forum page
        $forum = $this->ForumsFunctions->getForum($page_id);
    }
....
}

put the above in your index function for your forum controller and yes it will show up with the structure you want in the url.

eg this structure:

http://awesomecompany.com/client/plugin/my_plugin/forum/1/

that would get the forum id 1 as shown in the example url like you want.

do note the .... is to be your existing code don't copy the .'s please they have no real purpose other than being a placeholder for your other code. 

 

8 minutes ago, cyandark said:

Yes, but will be accessible from /forum/index/1/

If you want to delete the index part of the url you will need to make a route like this


Router::route("^forum/(.+)", "forum/index/$1");

You can take as an example the main controller of a previous version of BlestaCMS. It works in a similar way.

Thank you guys I've finally got where I want almost :D just need to fix that routes as it's working at the moment via forum/index/1/ but not /forum/1/ so I need to use the client_loc and plugins and then the folder name I think :) 

Link to comment
Share on other sites

4 minutes ago, BlestaStore said:

 

Thank you guys I've finally got where I want almost :D just need to fix that routes as it's working at the moment via forum/index/1/ but not /forum/1/ so I need to use the client_loc and plugins and then the folder name I think :) 

okay great, would be looking forward to beta test or get the plugin on release because I would like to pair blestacms and blestaforums together and move back from modx lol :D

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...