How to change directory when using a classtemplate

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

How to change directory when using a classtemplate

Post by hybris »

Hello again,

I use a class to help me with the layout of my homepage. To the left I have buttons that redirect to various php script (pages). Those are displayed in the main column on my page.

The mainpage class is stored in Framework_Lib.php and before I had all the pages in the same folder as Framework_Lib.php and it all worked fine.

The problem is there are so many files now so I wanted to move the pages the buttons point to to a subfolder called Leftmenu.

Now it only works the first time I click a button but after that all the links are broken since i jump from /Main folder (where the class is stored) to /Main/Leftmenu/

The way the page is setup is i make an array with the buttons and the url like $buttons = array ("Show Users" => "/../Main/Leftmenu/users.php");
In users I use
ob_start();
Code here
$content=ob_get_contents();
ob_end_clean();
$homepage->content = $content;
$homepage->Display();

So it all works fine when all the content is in the same folder but as soon as i put stuff in another folder it ofcourse breaks since my button array redirect to another folder.. but is there a way to get back to redirect back to the Main folder from the users.php page?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to change directory when using a classtemplate

Post by Celauran »

Is your Framework_Lib basically just doing an include of that page? You could set a variable as a base path so you can load things relative to its location. Really, though, feels like there's some reinventing of the wheel going on here. Any reason you're not using an existing router and something like Twig for templating?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to change directory when using a classtemplate

Post by requinix »

The browser won't go into the Leftmenu "directory" unless you tell it to in a link.
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

Re: How to change directory when using a classtemplate

Post by hybris »

Is your Framework_Lib basically just doing an include of that page?
Yeah. The Framework_lib just defines the layout and a row with top buttons and leftside buttons and what background imges to use in the different divs.
Really, though, feels like there's some reinventing of the wheel going on here.
I like wheels :D No the page is working fine as long as i dont try to put the files the menubuttons point to in a different folder.
Any reason you're not using an existing router and something like Twig for templating?
Well I want to learn and know what Im doing, also havent heard bout Twig before. But my template work fine if i just leave the files in the same folder. Was just curious if there were an easy fix like chdir() or something to jump back.
The browser won't go into the Leftmenu "directory" unless you tell it to in a link.
I do tell it to go there in the buttons array.. Was curious if there were any easy way to go back :)
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

Re: How to change directory when using a classtemplate

Post by hybris »

$root = realpath($_SERVER["HTTP_HOST"]);

Think that solved my problem...atleast with the links but now my graphics is screwed up... Fix 1 thing and break 2 other things heh
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to change directory when using a classtemplate

Post by Celauran »

Have you tried what I suggested earlier? Define your templates path in and relative to your Framework_Lib and use that to get the full path to use in your includes, rather than trying to juggle relative paths.
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

Re: How to change directory when using a classtemplate

Post by hybris »

I have some systems in earlier catalogues than the framework so I used $root = realpath($_SERVER["HTTP_HOST"]);
and $root = realpath($_SERVER["DOCUMENT_ROOT"]); instead and it worked like a charm.

Reason the graphics ddnt work was i forgot to update the img path but now it all works again :)

Thanks :)
Post Reply