How to make links through own PHP framework - php

I have my own made PHP Framework where the links in the browser are recognized like this.(my folder is called pro and it is stored in htdocs folder)
localhost/pro/data/entry
data: controller
entry: action
PHP Developers who use frameworks are familiar with this, and I can use this method without any problem. But the problems come when I want to make normal anchor links to documents, pages or stylesheets. When i do a normal link like this:
<a href="localhost/pro/application/view/data/css/style.css>
The browser considers controllers and actions and I need that they are seen like normal relative links?
How can i make normal relative anchor links without the browser to consider the controllers and actions.

Remove "localhost" and thats an absolute path.
<a href="/pro/application/view/data/css/style.css">
And if you want it relative to the script path ( let's supose its on the view folder ).
<a href="./data/css/style.css">

Related

Links not working from subdirectories

I want to make multiple pages on my website, but to keep everything clean I want to make different directoriess with the different pages. However, I use php to make a different file with my header that is included in all my pages, so I only have to change the code of my header once and it will be the same on all pages.
The problem is that the links I use in my menu items (like home, contact, about, etc.) will not work anymore when you're on a page inside a directory (I'll make an example below).
So my question:
Is there a home folder on a website (like ~/ on unix) or is there another way to make it work?
Example of my directory structure:
htdocs
index.php
header.php
menus
contact.php
about.php
(a link to index.php won't work anymore if you're on the contact.php page)
Sounds like you're using relative paths in your menu links. Use an absolute path instead by starting with a "/":
Home
About
or a complete URL:
Home
About
The home directory of a website can be accessed with a simple '/' at the start of the link you want to add. From there you can enter subfolders by appending the folder name.
Example:
'example.com/subfolder/subsubfolder/page.html'

How to code the navigation menu for sites with subdirectories?

I would like to understand how navigation works with sites with subdirectories.
For example, if I have this layout,
\index.php
\about_us\about_us.php
The navigation link for index.php to go back to homepage would be,
Home
Whereas the one in about_us.php would be,
Home
At the moment my solution is to simply put all the files in a subdirectory so that I can easily import the menu file into all the pages for easier management. For example,
\index\index.php
\about_us\about_us.php
I can then easily import a menu file,
<?php include 'menu.php'; ?>
...into all my pages since I only need,
Home
It doesn't seem like this is a good solution (everything in a subdirectory) for creating the navigation. Would like some advise on how this is done please. :) Thank you all.
In general, it is considered best-practice to use relative URLs, so
that your website will not be bound to the base URL of where it is
currently deployed. For example, it will be able to work on localhost,
as well as on your public domain, without modifications.
-Daniel Vassallo.
However, if you are pretty sure you don't need relative URLs, you can use absolute URLs. In your case, to link to homepage from everywhere with the same anchor tag you would use:
Home

How to make links work, regardless of location in directory structure?

So in the included image, you can see a copy of the website I'm currently working on. The site is going to have separate pages for each of the company's machines. Obviously the navigation bar and menu bar are going to be the same throughout the whole website so I thought I'd make use of a single line of PHP script to just load the menu in each time.
http://i.imgur.com/spVS3a4.png
(Attached image as I do not have +10 rep yet)
Though, from what I understand, the pages within folders (Not within root) wont be able to link to the rest of the site without the use of ../ within the href.
So if I load the menu part of the site containing the navbar, etc, it won't work on all the pages because they'll all be within different folders, meaning every folder will need it's own version of the navbar PHP import.
Am I going about this the wrong way? I'm just trying to minimize the amount of code I'm using on each page. Not only that, but once I finish the site if I have to make a change to the menu-bar, I don't want to have to update 30-40+ pages. I figured the PHP import would save me that sort of trouble? Is there a better way of doing this that isn't terribly complicated?
It should also be noted that I'm doing this in HTML5 with Twitter-Bootstrap as my main CSS.
I would also be making a similar PHP call that would import a footer as well.
If I missed anything that could be of help, please do ask.
TL;DR - How do I make links work throughout the entirety of the site, regardless of where they are in the folder hierarchy. Thought about using a tiny PHP script that would contain the menubar, etc and would be loaded on every single page. Though I'm not sure if that will work due to folders?
Try to start your links from /. This means that url is started from site root.
For example:
Patch Kettles
Just define a constant with the root path and echo it within all the links inside the pages that are contained in a folder like this:
[Put this in a file that you include through out your whole website (e.g. like the file with your database connection if you have one)]
define('BASE_URL' , 'http://'.$_SERVER['HTTP_HOST'].'/');
Than just add an echo in the start of each link:
Home
And for images the same thing:
<img src="<?php echo BASE_URL; ?>images/img_01.jpg"/>
You can just use /path/to/image.jpg to get absolute paths, while still being fine if your domain name ever changes.

subdomain changes image path

I have one dynamic website with back end control panel which contains ckEditor for data entry. ckEditor inserts images with path like: /userfiles/image1.jpg and when I call page to display on user interface it shows perfect if I access it from main url but due to some reasons few section we have to move on a sub folder and when I access same page from that subdomain it fails in displaying image.
when I checked url of the image, it shows like:
app1.mysite.com/userFiles/image1.jpg But the actual path is:mysite.com/userFiles/image1.jpg.
Is there any trick or technique to solve this?
I am using PHP.
You can define a <base> in your <head>:
http://www.w3schools.com/tags/tag_base.asp
To do ist with PHP is hard. Changing it beforehand to an absolute URL might be the best way, changing it afterwards means HTML scanning, finding the images, check wether the url is absolute or relative and change it, if needed.

Including headers and CSS in different directories

This must be a simple fix, but I just can't figure it out!
I am including the header via PHP into each new page, the header contains the CSS and scripts etc.
Only problem is since the page is in a different directory, when I link to the header like ../header etc it looks fine but the included scripts, are the included via a short URL e.g. /js/script.js
Which means on the page (in another directory) the scripts do not work!
I'm finding it hard to explain but take a look at this:
http://www.healthygit.com/
If you view the source all the scripts link fine.
Now look at this:
http://www.healthygit.com/fitness/running.php
If you try to click on a script to view it, it takes you to a 404 or in this case 302's you to the homepage.
Easy peasy, your source for the scripts, stylesheets etc. should have a / in front of them, that way the header file will always refer to the files from the root of the site.
I.e. this:
src="js/whatever.js"
Should become
src="/js/whatever.js"
This way it will always look for the files from the root of the site.
The same applies with CSS files, i.e. /css/whatever.css.

Categories