Welcome Guest, Not a member yet? Register   Sign In
Subdirectories in views problems with includes
#1

[eluser]cairo140[/eluser]
Hi all! I'm just starting out with CodeIgniter and playing around for a bit, and I'm having a bit of trouble with a couple view files. Calling all these view files from controllers.

My views folder is structured as follows (essentially):

views/
..includes/
....header.php
..corporate/
....add_view.php
about_view.php

In about.php, I call the following:

Code:
include_once ('includes/header.php');

It includes header.php no problem; however, when I write in add.php the following:

Code:
include_once ('../includes/header.php');

It just falls over and dies:

Quote:A PHP Error was encountered

Severity: Warning

Message: include(../includes/header.php) [function.include]: failed to open stream: No such file or directory

Filename: corporate/add_view.php

Line Number: 9

I must be missing something... any help would be much appreciated!
#2

[eluser]Colin Williams[/eluser]
I would stick to absolute paths, and use the APPPATH constant as a prefix.
#3

[eluser]bcorcoran[/eluser]
[quote author="Colin Williams" date="1233142535"]I would stick to absolute paths, and use the APPPATH constant as a prefix.[/quote]

Thanks for the tip about APPPATH.

Are there any advantages/disadvantages to using
Code:
include(APPPATH . 'views/viewfile.php');
instead of the load view method?
Code:
$this->load->view('views/viewfile');

What would be the reasoning for going one way or the other?

EDIT: I want to clarify that I am asking in terms of within a View file, NOT a Controller function.
#4

[eluser]jedd[/eluser]
My preference is for this approach, but only because it more closely matches the standard CI approach. Call it aesthetics, even.

Code:
$this->load->view('views/viewfile');

Quote:EDIT: I want to clarify that I am asking in terms of within a View file, NOT a Controller function.

Ahh, but you can't ask one question without getting two answers!

I adopt the approach discussed [url="http://ellislab.com/forums/viewthread/106945/"]in this thread[/url] where viewettes are loaded from within the controller, into variables, and then pushed into the primary view.

I guess the way you're doing it here could be described as loading views from within views. The approach discussed in that thread above emphasises the controller as being in control - view files don't call other view files. The hierarchy is easier to establish at a glance. I also think it keeps my view code a bit neater.
#5

[eluser]rootman[/eluser]
In a View POV i would use the APPPATH approach. Why? Simple! Think about splitting the work in a Team, and you got someone doing all the VIEW work, he surely wants to split up his code without having to ask the php team all along if they could load another bunches of views ...

I just stumbled across this Problem aswell and I chose the APPPATH way.
#6

[eluser]bcorcoran[/eluser]
[quote author="rootman" date="1237575332"]In a View POV i would use the APPPATH approach. Why? Simple! Think about splitting the work in a Team, and you got someone doing all the VIEW work, he surely wants to split up his code without having to ask the php team all along if they could load another bunches of views ...

I just stumbled across this Problem aswell and I chose the APPPATH way.[/quote]

Actually, he wouldn't have to. You can load views inside the view files themselves, which is why I asked in the first place.

Both seemingly achieve the same goal, but I went the APPPATH route as well because I think it will keep the processing overhead low, especially on high traffic, low cache sites.

I don't think it would be possible for $this->load->view(); to be faster than include() or a variation thereof. Possibly more flexible (concerning passing data to another view), but not more efficient.
#7

[eluser]rootman[/eluser]
Why would you pass data from one view to another? The data is passed from the controller and availiable in every other view you include. Thing is, the controller doesnt know that there are multiple files, it has to think of it as one view. Kinda black box thingy, dont ask how its done Smile

Another thing that would annoy me with $this->load->view() is that there would be too much coding logic in a view then. I like to keep views as simple as possible so my co-worker can understand them ;D I think they would start throwing things at me when i start to complicate things... Wink
#8

[eluser]bcorcoran[/eluser]
That's why CI gives you the flexibility to do it however you want Tongue




Theme © iAndrew 2016 - Forum software by © MyBB