I'm trying to create another page via a Controller. I'm reading through the documentation on this page http://www.getfuelcms.com/user_guide/general/creating-pages and what view is this page going to be looking at? I'm confused as to where the content comes from after looking at the example.
If you look at the section labeled "Using Controllers - about/contact" it renders the page using the Fuel_page class. We use the Fuel_page class so that it will import all the variables from the _variables files associated with the page (if any) and if the page is created in the admin, any variables from the fuel_pagevars table. The location value passed is the key to grabbing all the variables.
Alternatively you can use the native CI $this->load->view('my_view', $vars);
So are you saying that if I have a file controllers/aboutTest.php and in that file I have <?php Class AboutTest extends CI_Controller {
function __construct() { parent::__construct(); }
function index() { $vars = array('page_title' => 'Contact : My Website');
//... form code goes here
//load the fuel_page library class and passit the view file you want to load $this->load->module_library(FUEL_FOLDER, 'fuel_page', array('location' => 'aboutTest')); $this->fuel_page->add_variables($vars); $this->fuel_page->render(); } }
It is going to be looking for content in the file views/aboutTest.php and it's going to look for variables to use for this page in views/_variables/global.php?
If you have no page in the admin with the location of 'aboutTest' It will look for a view file that matches the location path.
Variables can come from a few places here: 1. Your $vars variable in the index controller method 2. Any variables in the views/_variables/global.php 3. Any variables in the views/_variables/aboutTest.php that match that location (read the following to learn more about the difference between page variables and controller variables) http://www.getfuelcms.com/user_guide/general/opt-in-controllers 4. If the page was created in the admin (which takes precedence over a static view file), then it would additionally look for variables in the database associated with that page (e.g. page title, meta description, body, body_class..etc)
Also, I'd recommend not using camel casing for the controller names and view files and instead use underscores for the controller names and either underscores or hyphens for the view files.
//load the fuel_page library class and passit the view file you want to load $this->load->module_library(FUEL_FOLDER, 'fuel_page', array('location' => 'aboutTest')); $this->fuel_page->add_variables($vars); $this->fuel_page->render();
The only way I can view this page is to first add it in the admin side. If I wanted to view the page without adding it to the admin side, I would just do the regular old $this->load->view('aboutTest');
No... you don't need to add it to the admin. Using Fuel_page will check first to see if a page is created in the admin (if the fuel_mode is set to 'auto') and if not, will proceed to look for a view file at the uri path location. It will also gather all the variable information.
You can use $this->load->view(), however, you won't be able to take advantage of the variables in the _variables file as easily.
Interesting....Well i've created the controller I posted above, created a view file with the same name, have NOT added it to the admin and when I go to the url, I get a 404 error with the main template. I haven't added the page to the admin side.
I created a new controller and copied the same code from above and called it 'Something' so the URL should be /something . Still no echo'd content showing up.
And in your index method you echoed out something correct? Do you have this problem with just a normal CI installation? ....
function index()
{
echo 'test';
}
...
If I browse to index.php/something I get the main template with a 404 error for the content. Yes I have the .htaccess file setup that comes with the fuel package.
Ok so i'm on a different computer now and I can echo content from the controller and the view loads just fine. I'm not sure why it's not working on my other computer.
So are you on a different computer looking at the other computer's site or did you do a different setup on your other computer?
Where on your server is FUEL installed? If it's at the root of your web directory then the .htaccess that comes with FUEL should be OK. If not, you'll need to change the RewriteBase to the appropriate sub-directory if you haven't already.
Different computer, different FuelCMS setup. I'm on mamp right, I was on xampp earlier. I'll try messing with the .htaccess file on xampp on Monday to see if that's what my problem was.
Comments
Alternatively you can use the native CI $this->load->view('my_view', $vars);
Class AboutTest extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$vars = array('page_title' => 'Contact : My Website');
//... form code goes here
//load the fuel_page library class and passit the view file you want to load
$this->load->module_library(FUEL_FOLDER, 'fuel_page', array('location' => 'aboutTest'));
$this->fuel_page->add_variables($vars);
$this->fuel_page->render();
}
}
It is going to be looking for content in the file views/aboutTest.php and it's going to look for variables to use for this page in views/_variables/global.php?
Variables can come from a few places here:
1. Your $vars variable in the index controller method
2. Any variables in the views/_variables/global.php
3. Any variables in the views/_variables/aboutTest.php that match that location (read the following to learn more about the difference between page variables and controller variables)
http://www.getfuelcms.com/user_guide/general/opt-in-controllers
4. If the page was created in the admin (which takes precedence over a static view file), then it would additionally look for variables in the database associated with that page (e.g. page title, meta description, body, body_class..etc)
Also, I'd recommend not using camel casing for the controller names and view files and instead use underscores for the controller names and either underscores or hyphens for the view files.
//load the fuel_page library class and passit the view file you want to load
$this->load->module_library(FUEL_FOLDER, 'fuel_page', array('location' => 'aboutTest'));
$this->fuel_page->add_variables($vars);
$this->fuel_page->render();
The only way I can view this page is to first add it in the admin side. If I wanted to view the page without adding it to the admin side, I would just do the regular old $this->load->view('aboutTest');
Am I understanding that correctly?
You can use $this->load->view(), however, you won't be able to take advantage of the variables in the _variables file as easily.
.... function index() { echo 'test'; } ...
p.s. how do you make your code have the yellow backround like that in this forum??
Use the "code" HTML tag for the yellow background.
Where on your server is FUEL installed? If it's at the root of your web directory then the .htaccess that comes with FUEL should be OK. If not, you'll need to change the RewriteBase to the appropriate sub-directory if you haven't already.