Welcome Guest, Not a member yet? Register   Sign In
Help to include CI output (menu) on another non-CI webpage
#1

[eluser]Chris Papaioannou[/eluser]
Hey guys

Building a website at the moment in CI which I'm relatively new to. The website has a menu which goes across all pages, this is formed up using the following snippets of code (will only put whats relevant).

In the constructor of every controller
Code:
$this->data['menu']       = $this->load->view('menu', array('menu' => $this->Menu_model->get_menu()), TRUE);


Followed by this in the actual page functions
Code:
$data = $this->data;
$this->load->view('layout', $data);


The Menu_model->get_menu() just returns an array of data to the 'menu' view.

This setup works fine on the website itself, though I'm sure it could be done better/more efficiently its not my main concern at the moment.

There is a forum which runs alongside the website (Invision Power Board) and I want to include this menu on there too. My current though is that this is going to be a bit tricky, due to the need for a controller, model and view to all be used in someway to help generate the menu output. I've created a controller dedicated to outputting the menu only which contains the following code

Code:
<?php

class Menu extends CI_Controller {

function __construct(){

  parent::__construct();
  // Load configs
  $this->config->load('general');

  // Forum integration
  require_once($this->config->item('IPBWI','includes'));
  $this->ipbwi = clone $ipbwi;

  if($this->ipbwi->member->isLoggedIn()){
   $this->member = $this->ipbwi->member->photo(null,true,true);
   $this->load->vars(array('member' => $this->member));
  }

  // Common models
  $this->load->model('Menu_model');
}

function index() {
  $this->load->view('menu', array('menu' => $this->Menu_model->get_menu()));
   }
}

Viewing this at 'www.site.com/menu' works fine, but I'm stumped at how to get this output into an external PHP file. I'd have to include the CI index.php and pass it parameters to get the right controller showing.

Current solution is to put this in a standalone file 'menu_include.php'

Code:
<?
// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://site/menu");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch,CURLOPT_COOKIE,$_SERVER['HTTP_COOKIE']);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);

?>


And then to go into the invision skinning and use the following line, because I believe its quite tricky to even get a php include working through IPB and you have to use their custom syntax.

Code:
{parse include="../menu_include.php"}

This all works fine, but I don't think I should have to be using curl to be able to grab the output of the menu controller as if I'm an external visitor. How on earth can I streamline this so its not such a clusterfuck of code. Have spent a fair time on google looking at various solutions but presume I'm describing the issue badly in my queries.

TL;DR. How can I include the complete output from a CI controller, into another non CI page through use of internal includes rather than external curl/file get contents style includes.

Thanks for any help!





#2

[eluser]Chris Papaioannou[/eluser]
Not sure what the bumping protocol is on here. Have waited till its at the bottom of page 4 though Sad
#3

[eluser]JoostV[/eluser]
You could just have a controller spit out the entire menu and load it into a dedicated <div> in the forum using jQuery post();
#4

[eluser]Chris Papaioannou[/eluser]
Trying to avoid extra HTTP requests to the server where possible so would like to avoid anything which makes an external request like including through Javascript or PHP file_get_contents/cURL style functions.

Thanks though
#5

[eluser]CroNiX[/eluser]
There are lots of ways to do this, really. The simplest might be to have CI generate and save a html file for the view menu. When in ci you can just have it load this file(not true view file) (which gets created when creating/editing the menu). Then in the forum software just include() that file where it needs to show up.

You could also create a menu system in Invision that just queries the database and assembles the menu, just like you are in CI. This method would be a duplication in code, though.
#6

[eluser]Chris Papaioannou[/eluser]
Thanks for your post CroNiX. The reason I can't (or at least can't see a straight forward way of doing so) generate the HTML and then include as a file from IPB is that the menu view has a bit of logic/PHP to put user specific links in (to their profile for example).

The data to make up the rest of the menu is cached from the database to reduce load but as for the entire menu, I can't simply cache a file copy and include it elsewhere Sad

Second suggestion has not been done for those exact reasons, don't want to duplicate code anywhere and I'm sure there might be other items in future which I'd want CI to generate and then include in a forum sidebar etc, so I'm hoping to find a way around the issue rather than avoid tackling it if that makes sense.
#7

[eluser]CroNiX[/eluser]
Well, you have really limited your options, then. No matter what you do, something will be inefficient with this setup, so its really deciding between the lesser of evils.




Theme © iAndrew 2016 - Forum software by © MyBB