Include files in Codeigniter - php

Where is the "best practice" place to put include files (inc.php) when using the Codeigniter framework?

Since you are asking for "best practice", let me give you a long answer:
It's depend what is your included file is about. Since you are using a framework,
CodeIginter have defined various
places for you to place your library,
helper, config, template, etc. It's
better to stick to what the framework offers
or you won't benefit from it.
It's all about the maintainability, the structure of your app, etc.
Don't use a framework unless you are intend to understand them. Use library instead if you are only looking to reuse functionality that people have written.

Mark, put your include files in the view directory as long as they are html, xhtml, php etc. You can make directory's under the view directory to keep files in as well.

yes u can by include Path Helper in your controllers file:
$this->load->helper(array('path'));
then load your file using function file():
$this->load->file('your_folder/your_file.php');
Note that your_file.php file is in root directory in your_folder dir.

I think as a general rule of thumb only include a file where and when you need it.
in terms of config files you'll want to include these as the very first file at the top of the page. This will ensure that any constants and configuratios are available for the rest of the scripts execution!

Related

Is there a possible solution on how to hide file name in PHP or .htaccess?

I want to change my url path to this:
localhost/sampleproject/validation.php
to
localhost/sampleproject/
I want to secure my file path. is there a code in .htaccess? if there is I will create .htaccess but i have no clue where to start, still learning to php thank you
that type of change usually hides index.php if you need to run code in a validation.php i might suggest including it within your index.php you can do so conditionally if necessary using include() or require_once() if you do that the user will have no knowledge of that file on the fronted. here's some examples of that https://www.w3schools.com/PHP/php_includes.asp
reference the links in comments of your question but as I said generally with apache you can configure it to not show the index.php but other than that it's per folder basis.
you may also want to look into creating some routes to trick the browser abit if that's your approach.
here's an example http://blogs.shephertz.com/2014/05/21/how-to-implement-url-routing-in-php/
or you can always use a framework such as symfony or Laravel which have route implementations for you to use.
Happy Coding.

PHP Bootstrapping Basics

I'm writing my first PHP app. Everyone talks about having a bootstrap.php to initialize your app. This makes sense and I've put together one that I'm happy with. There's two things I don't understand, and which no one seems to mention:
Where do I call my boostrap from? Do I include it in every page? Please tell me there's a better way...
How do I make it so my bootstrap is not called more often than needed? I assume it only needs to be called either at the start of the app or the start of a new session.
1: Generally the bootstrap is in the "application" directory. It is called from the "public" directory which is in the same level as application (not inside of it). The index.php inside the public folder should include the bootstrap.php and that is where your Document_Root should be set to (note you may need to change / add some include paths for it to work)
2: It should only be included once via the index.php file in the public folder. Just including it there should be enough, if it was done correctly.
Hope that helps.
It depends on your application architecture.
If your architecture is the good old "flock of php scripts" - PHP scripts called directly from the browser - then you'll be including it at the top of each script, one way or another.
Most developers (and frameworks) these days marshall all their requests through /index.php one way or another, usually with some URL rewriting going on to make nice, pretty URLs for users to see.
In this day and age, you should probably be doing the latter, or at least thinking about it. It leads to much better organization, and even more importantly, allows you to keep all your code outside of the web server's document root, which is a good security practice for several reasons that are outside the scope of this answer.
Have a look at the singleton pattern. You can double your bootstrap class as a resource container, e.g.:
$bootstrap = Bootstrap::getInstance();
$dbConn = $bootstrap->getPdoDbh();
You can include or require the file, or use the autoloader and make sure you have a call to instantiate the object on all your pages. You might even have a call to getInstance() on the bottom of the file, after the class definition.
Or you might use URL-based routing and have all your requests go through a single index.php file, like Zend Framework does. Or better yet, use Zend Framework.
This answer assumes you're doing OOP w/ PHP >=5, which really is the way to go.
One of the more elegant means by which to bootstrap a PHP application is to do so using Composer.
Almost every PHP library uses Composer nowadays, and requiring a Bootstrap.php-like file is as simple as:
"autoload": {
"psr-4": {
"Acme\\Rocket\\": "src/"
},
"files": ["src/Bootstrap.php"]
},
Note the second property, files. (The first, psr-4, is standard PSR-4 boilerplate auto-loading, and is included only to make the example more real-world.)
Including the bootstrap file in this way doesn't make the naive assumption that the PHP application is executed in a web-server context, via index.php, or similar; the application could very well be a command-line application (or both, like Laravel/Artisan). Bootstrapping via the auto-loader makes this distinction a non-issue.
it depends on what your bootstrap file does. If it's just a file that sets some ini settings and such to create a sane execution environment and establish a database connection, then simply including it with require_once in your scripts should be enough. If it's more of a single-point of entry then you can configure your server to filter all requests to it and have it dispatch to appropriate controller scripts.

How to keep relative filepaths intact, even though I'm bootstrapping

I'm trying to include a premade messageboard, Phorum, into one of my Zend projects. Phorum is a relatively large and intricate web of PHP includes. My website already has a signup, so I'm trying to combine the two login systems into one.
What I've done is to make a controller that includes the Phorum index.php. This will let me use the authentication system I've set up. My problem is that, since I'm bootstrapping, all the relative filepaths within Phorum's index.php seem to try to begin at my Zend project's index.php, meaning they all seem to break.
Is there a way around this? Should I include? Render? Something better?
All help is appreciated.
Adding the appropriate chdir (back to Phorums include path root) in your Bootstrap file should do it. Then you have to of course take care that you Zend App uses application specific and not relative paths.
You can use the include_path setting, which can be set at runtime using set_include_path(). This doesn't require you to change the working directory, but makes PHP search for includes in all the directories specified in this setting.

What is the best way to set up shared php script files on a webserver?

I'm running the same php script on many domains on an apache2 server. Only some of the files change between domains, and the rest are always the same. Right now, every time I set up a new domain, I copy all the files, but I'd really like to have these common files in one place so any changes would affect all domains.
I've thought of using a bunch of symlinks to point at common files. Is this an ok approach, or are there some simple edits I can make to the php scripts or apache configuration files to make this more efficient?
Thanks!
The way I do this kind of thing is to create a "common" directory, where I place all the file that can be shared between each site. Then I simply include them wherever they are needed.
This is pretty good because allows to add features across multiple sites.
I'd suggest abstracting the common code into a set of 'library' scripts. Placing these in a common directory, and making that available by modifying PHP's include_path variable. This means you most likely won't have to modify your current scripts, while still removing the need to have more than one copy.
This path could (and probably should) be outside of your public directories. This enhances the security of your websites by not making them directly available to outside users.
This can be a bit tricky, as the application almost needs to know you're doing this. IME, it works best when you can divide the app into common code and instance code in two separate directory trees. The common code also needs to not do anything silly like include a file that has to be in the instance tree.
A single point of entry to load the common code is also a big bonus because then you can chain a few very small files: the instance code includes one in it's own directory; that file includes a file outside the instance code; that file then either loads the entry point file for the common code, or loads another that does. Now this is only one way to do it, but it means you have just one file that needs to know where the common code is (so you can move it if you have to with minimal effort), and if you do it right, all the various instance code trees load it, albeit indirectly.
You could have a library directory that sits above all of your sites, and a config file that states which library files your sites should include by default. You can then have another config file within each site that overrides the global config. These config files can be used to generate include('../../lib/*.php') statements to build the basic function toolkit needed for each site.
some_high_level_directory/
-> lib/
->*.php (library files)
-> config.php (global library includes)
-> site_1/
-> config.php (library includes that only relate to site_1)
-> www/
-> site_2/
-> config.php (library includes that only relate to site_2)
-> www/
-> etc, etc
Hopefully that makes sense... :)

Include Scripts library into Symfony application

I have a directory of PHP scripts ( mainly utilities), that is located on C:\Development, whereas my Symfony application is located in D:\Development.
How to include the utility scripts into the Symfony application? I try not to use include() because I prefer a symfony-like command.
There are basically two approaches
Extend the autoloading
modify the include path
#2 can be achieved numerous ways. If you are unsure how to, let me know and I'll post some details.
You don't have to change php.ini. You can use ini_set or set_include_path. Check ricardo's comment in the User Contributed Notes section at the documentation for set_include_path().
Personally, I'd create a new folder inside the lib/vendor folder on your project, put your scripts in there, and then symfony should handle it all automatically assuming you've followed its naming conventions for files/classes. If not, you may need to alter the autoload stuff as per Peter Bailey's answer.
I prefer this way because I like to be able to deploy the project from one server to another as one atomic unit with no reliance on external dependancies. This may not be an issue for you though, in which case the existing answer will suit you better.

Categories