Need php template help and clean url help - php

I'm new to PHP and web development in general, but have been a programmer for 5 years. I'm trying to work on my own website from scratch using Notepad to edit my PHP files and WAMP for my server. I'm able to view my files fine in Safari, Chrome and Firefox, but not IE (which we all know IE isn't the greatest) because I'm using some HTML5 stuff. Anyways, I have an Includes folder that holds my files for my header, menu and footer. I have an index.php file that includes these files and displays them fine. In the center of the page is where I want the content. To try and keep clean urls, I made quite a few folders and put this same index.php file in there (e.g. Profile/index.php, Forums/index.php, etc.). I did this so when I went to localhost/mysite/profile/ it showed me the template I wanted to use. However, there has got to be a better way to use the template and a better way to have clean urls. I'm not currently hosting this site anywhere so I don't know if I'll have access to the htaccess file (not even sure what it is honestly, just seen it mentioned), but I was curious of having the folder structure (one folder for each menu item) is a normal or ok practice? I was also curious if there is a way to use the index.php without having to copy and paste it every time I make a small change. Any help would be appreciated. Thanks.

If you are planning on templating I suggest using an existing platform like Symfony, Zend Framework, or Smarty. Smarty is probably the easiest to get going with and it is (almost) purely for templating. Whereas Symfony and Zend Framework are big packages with lots of bells and whistles.
If I was going to be doing pure templating I would look at Smarty. I use Zend Framework for just about all my current PHP projects now but it has a pretty steep learning curve. Your first couple weeks will be frustrating.
As far as URLs go, .htaccess is probably the preferred method (at least in my book). Zend Framework and Symfony both have kind of default URL writing style that looks like http://host/controller/action where controller would be Profile or Forums. You wouldn't necessarily have to see host/profile/index, it could be host/profile or host/profile/edit, where edit is the action being performed.

Copying the index.php file is not really the way I would go. It seems messy and there are a few other options. If you want the urls to be clean and search engine friendly you should really have a look at url rewriting using .htaccess
You're saying that you're not sure if you will have a server with "access to the htaccess file" but if you can upload files you can always upload a .htaccess file as well -- the problem is that the web server is not always using them or might not have mod_rewrite enabled. In this case you can get your urls on the format of http://www.example.com/index.php?u=/Profile/foo (this is also my preferred way to handle url rewrites).
In your index.php just make sure to read the requested url parameter passed by mod_rewrite and include whatever files in the folder that you need. Actually, you don't even need a "physical" folder structure, you might want to implement it using classes or something like that. (I'm sure I would).
I would really recommend that you go have a look at a good PHP framework, CodeIgniter for example. Even if you decide to code everything from scratch, you would still learn a lot about best practices for url handling, databases, the MVC pattern, template engines, database abstraction layers and PHP coding in general.

your answer is the htaccess file, is converts the 'folder structure' to $_GET value's
for example,
if you're in website.com/Profile/ you can write an htaccess line that will convert that into website.com/index.php?folder=Profile
htaccess:
RewriteEngine on
RewriteRule ^/(.*)/$ index.php?folder=$1 [NC,L,QSA]
RewriteRule ^/(.*).html$ index.php?folder=$1 [NC,L,QSA]

Related

All URLs are redirected to index.php

I have setup a Symphony framework on my localhost using this tutorial. I am using the PHP default server and MySQL.
The frontend URL is working fine but when I go to the admin URL (http://localhost:8000/symdemo/admin), then my CSS URL is also redirected to the admin page meaning I am not getting CSS code into the response.
The reason as that the default PHP server does not have a URL-rewriting module and index.php is also adding to the CSS path.
How can I fix this?
I'm not 100% sure, but I think you'll find that using a webserver that supports rewrites is quicker than trying to replicate the necessary rewrites in PHP. Rewrites are listed as a requirement in Symphony CMS's readme, and last time I checked Symphony was still dependent on webserver rewrites for some of its routing/files.
You can, of course, use Apache. If you'd like to use a lighter and cleaner webserver that's easy to configure, I recommend Hiawatha, which has a Symphony URL toolkit/rewrite rule set available.

How to create a proper routing system for a no-framework PHP web application?

I am a beginner in PHP development, and I have recently been asked to create a small web application from scratch. I did some research and I decided that I DO NOT want to use any framework, because I truly want to learn how to create decent code myself.
The thing is, I am creating a basic MVC structure to generate a basic CRUD for a model that I have, but I am totally clueless about the best practices to get clean, useful URLs for my application.
I am running on Ubuntu 16.04 LTS, using Apache as a webserver with PHP 5.6 and MySQL. My first idea was to redirect all the requests through index.php and arrange some sort of mechanism in an .htaccess file to rewrite ugly URLs into more friendly and simpler ones, but to be honest, I do not even know where to begin with.
How am I supposed to handle the requests in my index.php file to redirect the user to the proper Controller and do the required action in there? Am I taking the right path regarding this?

Partially integrating the Laravel Framework in select sub-directories of a main website

I recently asked a similar question and received some great answers, but I'm realizing that it wasn't quite the right question.
We currently use a CMS that fulfills 95% of our needs on our site, however for that extra 5% I'd like to bring in a PHP Framework for things like Form Processing/Validation, Authentication, etc. My original question was if it would be possible to have a single sub-directory within our site that could manage all of those projects. But now I'm realizing that this method would need everything to be in something like a .../projects URI, breaking a pretty good REST setup we have now with the CMS (the only routing I'd want to have to manage is within my own applications, not the entire site to work around this).
Is it possible to have Laravel's main folder at the root directory, and only include it in specific sub-directories or files that I want. I imagine it something like just having multiple public folders, which I'm not sure if it's possible/reccommended? I also imagine I would have to essentially turn of Laravel's Routing so that it doesn't step on the toes of the CMS, but I believe I could turn it back on for each folder with each having its own .htaccess?
I picture the architecture looking sort of like this:
root
/ \
html laravel
/ \
... [normal page]
/ \ \
/ \ [project]
[project] [normal page]
I've read a little that the Zend Framework is made especially for partial integration like this, in which case if it's better suited for the job, I could definitely switch over. But it'd be great if it'd be possible with Laravel since I'm already fairly comfortable with the basics.
You should be able to do this fairly easily. The basic steps include:
Modify your .htaccess or vhost files so that each location hosted by Laravel points to Laravel's public/index.php. You'll need to put these more specific rules first, as Apache will follow the first matching rule that it finds.
[Optional] If you want to change the folder structure of your Laravel project, modify the files in the bootstrap folder (paths.php, autoload.php, and start.php) to point to the correct paths, and make sure that public/index.php (or wherever you put it) can find the autoload and start files.
That should be it. Make sure that your Laravel routes reference the full path.
Unfortunately, this approach does mean you'll need to add a rewrite rule for each Laravel route you want to add. This way, Laravel won't interfere with your CMS's routing; it will only handle requests that get rewritten to go to index.php. You just have to make sure that the rewrite rules have higher priority by putting them before the general rewrite rules for your CMS.

Website structure / Newbie

I created a website using php, passing values from page to page either with POST or GET.
Though there is some cons, I dont know how to track specifically what data has been viewed in GoogleAnalytics as it just shows the name of the page (xxxx.php)
On the other side, I see websites structured differently with a bunch of subdirectories created : www.xxx.com/xxxxxx/xxxxx/xxx
This looks like pretty manual for me , compared to the .php?xxxx=xxxx way of structuring.
Do you know how this subdirectory style structuring can be automatically obtained?
This is done with Apache rewrite rules.
To make it so that whenever a user goes to /posts/visiting_new_york, it actually goes to to /viewpost.php?id=visiting_new_york, you create a file in your site called .htaccess like this:
RewriteEngine On
RewriteRule '^posts/([^/]+)$' viewpost.php?id=$1 [L]
Use an MVC framework like rails, or simply configure your webserver's virtual directory structure to be identical to the local servers file system and adhere to that scheme when saving your php files.
Yes, you can do this with "mod_rewrite" in apache.
It involves creating a .htaccess file with URL re-writing rules inside.
So you can transform /index.php?page=contact&lang=en into /en/contact/
Here's a good rewrite cheat sheet: http://www.addedbytes.com/cheat-sheets/mod_rewrite-cheat-sheet/
Wadih
You need to read about url rewriting
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
If you just want to track your dynamic pages , there is another solution in Google analytic
http://www.google.com/support/googleanalytics/bin/answer.py?answer=55504

Which one is faster rewrite or folder based URLs?

I am creating URLs in my website as
http://example.com/register/
http://example.com/login/
I am creating above URLS by writing following rules in htaccess
RewriteRule register/ /register.php
RewriteRule login/ /login.php
Now its working fine for me, but if I create my URLs by creating seprate folder for /login/index.php and /register/index.php By creating folders and index.php files inside those I can achieve above functionality easily.
But I want to know which w=one will be faster, I tried both methods but not seen much difference, according to logic and apache specifications and everything which method will be faster and a good method to go with.
My friend says .htaccess rules will be slower, because in case of htaccess first it will check for rules and then it will redirect to the corresponding attached page, this process will take time than folder organization.
I know that .htaccess will at least be easier for development/debugging purposes because:
1) Because all of your files will be in the same directory instead of their individual ones.
2) Because your editor will show the actual filenames instead of multiple index.php.
Also, since you're using .htaccess, this makes it easier to put a rule in such as:
RewriteRule ^getdata/(\d+)/$ getdata.php?page=$1
Which you can't do when using the directory-based methods.
Overall, the speed difference is negligible compared to the benefits available from using mod_rewrite.
.htaccess will be slower, but the time difference will be negligible.
You should concentrate on other things - like which one is easier to maintain.

Categories