Michael Posted February 17, 2018 Report Posted February 17, 2018 Hi there guys and gals, I was wondering if anyone knows how to set content by grabbing the id from the url? Like blesta/page/1/ returns content related to the id 1? Thanks. Quote
Abdy Posted February 17, 2018 Report Posted February 17, 2018 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); Michael 1 Quote
timnboys Posted February 17, 2018 Report Posted February 17, 2018 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. Michael 1 Quote
Michael Posted February 18, 2018 Author Report Posted February 18, 2018 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. Quote
timnboys Posted February 18, 2018 Report Posted February 18, 2018 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. Quote
Michael Posted February 18, 2018 Author Report Posted February 18, 2018 4 minutes ago, timnboys said: 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. nothing in my public_html's mate. Quote
Abdy Posted February 18, 2018 Report Posted February 18, 2018 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); } } Michael 1 Quote
Abdy Posted February 18, 2018 Report Posted February 18, 2018 You can also print the $this->get variable to check the content with print_r($this->get); exit; Michael 1 Quote
Michael Posted February 18, 2018 Author Report Posted February 18, 2018 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? Quote
timnboys Posted February 18, 2018 Report Posted February 18, 2018 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. Michael 1 Quote
Abdy Posted February 18, 2018 Report Posted February 18, 2018 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. Michael 1 Quote
Michael Posted February 18, 2018 Author Report Posted February 18, 2018 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 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 Abdy and Jono 2 Quote
timnboys Posted February 18, 2018 Report Posted February 18, 2018 4 minutes ago, BlestaStore said: Thank you guys I've finally got where I want almost 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 Michael 1 Quote
Abdy Posted February 18, 2018 Report Posted February 18, 2018 4 minutes ago, BlestaStore said: Thank you guys I've finally got where I want almost 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 Great Michael 1 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.