It appears that this question is asked often and answered the same way: store the css files outside of the application directory and then use base_url() . "path/to/file".
However, I want to keep my css files and js files inside my application/views/ directory, because the views directory is effectively the html space, and css and js belong to that space (in my opinion).
Below is the structure that I wish for:
root
- application
-- views
--- assets
---- css
---- js
- system
When I attempt to load css files from within this directory structure, I get a NetworkError: 403 Forbidden, which makes sense because of CI's framework protocol.
But I am guessing that there is a way.
Publicly reachable files like CSSes images and JS files need to be in public directory next to index.php file. So hierarchy would be:
root
- application
- system
- assets
-- css
-- js
You can aproach to those files with hard coded
Link
or using (loaded) url helper with it's function base_url() or site_url(). Don't forget to fill correct URL into application config file.
Link
Docs.
Hey i'm going to politely push back on this idea :-)
Your application and system folders should be ABOVE the root, so they are not publicly accessible. (Unless this is a really simple application and you are not doing any database interaction, etc). They should not be considered part of the HTML or public space because you do not want the public accessing them. Set the path for them once in the main index.php file and its done.
Also i suggest renaming your system and application folders, like "system302". Over the long term it makes versioning and upgrading (and reverting if needed) much easier.
Related
I am trying to use AJAX to return high-level calculation results. However, it appears as though the PHP file that I am pointing the AJAX call to is in the '/' root directory. Here is a snapshot of how my structure is: c:\webserver\test\webroot (this is the root directory), and then c:\webserver\test\code is where the HTML and PHP files are stored - looking to use an AJAX call to point to a PHP file in the non-root folder (which is one folder up, then one folder down). I tried a few different things, such as '../code' to move up and then over, but that doesn't work. Any suggestions?
Referencing a file location via relative paths should work - you'll just need to be sure that you are navigating FROM the correct folder TO the correct folder.
This can be a bit tricky if your javascript file is located in another folder - if memory serves you must navigate from the folder containing the javascript file, not from the folder containing the index file.
Alternately, you can use FQDM http://example.com/folder/folder/file.php to reference the file. Note that if you are on localhost, you can use a hosts file to fake-out the webserver.
I am using shared hosing for Codeigniter application for deployment.
How can i run staging url on live server?
1) domain.com (Working)
2) domain.com/staging/ (Not working, showing 404 error)
I created staging folder under public_html directory, now put All CI code in staging folder, and tried to run the application with URL domain.com/staging/ but it redirect on domain.com/404
I expect to run domain.com for users and domain.com/staging for development. (Staging URL i will use for development and after that i will place the same code for live URL.)
This is most easily done by using a subdomain for the staging version, i.e. staging.doman.com. The folder where you put the CI files isn't super important as the subdomain will be set to use the directory you choose.
You will either need to have complete control over the server or have a service provider that allows you to set up subdomains.
- Expanded Answer -
If you cannot set up a subdomain then your next best option is to use the advice in the documentation on Managing you Applications. It isn't required to use the file structure shown there. In your case try the following.
Create a folder on the same level as application named staging.
Into that folder copy all the folders and files normally found in /application.
Add all application folders and files required by your site. The files in these folders are those that make up the application you are staging.
Make a copy of index.php (the file at the root of the public folder, i.e. domain.com/index.php) and name it staging.php. Both index.php and staging.php will be in the same folder.
Edit staging.php and change the value of $application_folder to be the absolute path to the staging folder. For example:
$application_folder = '/path/to/domain.com/public_folder/staging';
The path above is an example. You must replace it with the actual path in your server. If you have no idea what the full and absolute path is you can use
$application_folder = dirname(__FILE__).'/staging';
Browse to the URL domain.com/staging.php and you should get your default controller's output. To test that you are actually seeing the staging version of the page temporarily change something in the view file so there can be no doubt.
You will go to other controllers by using the URL domain.com/staging.php/other_controller.
Change the value of $config['index_page'] in /staging/config/config.php to staging.php, e.g.
$config['index_page'] = 'staging.php';
Without the above change redirect() and many other "helper" functions will not work correctly.
(Optional?) You can probably edit the main .htaccess and add rewrite rules for staging.php the same way it is done for index.php. But I will leave that as an exercise for you. (Or for another SO question.)
A final word. It might be necessary to make other configuration changes to accommodate the revised file structure. I tested the answer here but did not go beyond the most basic task of browsing to controllers. Most likely config values for sessions, cookies, and many others will need to be adjusted for the staged version to work correctly. Check out the documentation on Handling Multiple Environments for guidance on making this less painful.
I am working on a CodeIgniter - HMVC
What I know:
is I should not create assets like css,js,images inside the application folder in codeigniter, but I have a necessity for it.
**What I am trying to achieve is : **
to create a standalone module and trying to use it in another project. In that case, I really need to add the necessary css,js,images files into the modules folder it self.
The Error I get is:
You don't have permission to access /v7-bitbucket/application/modules/some_modle/assets/bootstrap.min.css on this server.
How do I overcome this?
Please advice.
Thanks.
There are 2 ways I can think of for you to access this data.
The first one is using an htaccess and a custom rule, which redirects certain files to inside the other directory. This way, the user will still have access to only certain specific files outside of the public HTML folder.
The second option is to use PHP and render the content of the file in custom tags.
Ex: Instead of entering <link href="<?= $path ?>">, you would do <style><?= file_get_contents($path) ?></style>. This would be harder to cache, and a bit harder to implement for images.
I have created my site with controllers such as about, products etc... which gives me example.com/about/ etc..
How would I create a admin section with the same controller name, like example.com/admin/about or example.com/admin/products ?
How do I organize my controllers?
2 more options to compliment WebweaverD answer.
Use Modular Separation
Create a second application folder and index file, that will be responsible for admin, connect them to the same system folder.
The second option is really easy to manage once you have set it up. There are variations but I find the structure below to be the most convenient.
mykewlwebsite.com
apps/
frontend/
app/ frontend codeigniter application folder
public/
index.php
assets/ frontend js, css, images
backend
app/ backend codeigniter application folder
public/
index.php
assets/ backend js, css, images
config/
database.php
constants.php
system/ codeigniter system folder
The database.php file contains the code from CodeIgniter's config/database.php and is shared for all applications of your project - simply remove all code and add require_once('../../../../config/database.php'); to the config/database.php
index.php files inside public folders have two important variables $system_path and $application_folder, change them to
$system_path = '../../../system/codeigniter';
$application_folder = '../app';
constants.php file can have some constants like the ENVIRONMENT constant from the index.php files and some other. Just require_once() it from the index.php files.
Though there are pros and cons.
PROS
For those of you, who are developing on localhost and deploying to servers via FTP or other systems can simply upload one folder - apps and overwrite the target folder without fear of overwriting database.php settings (I'm sure most of you have them different from the local ones).
Adding one more application is easy - just duplicate one of existing. You can add as many applications as you want - api, ajax, user cabinet, etc.
CONS
This structure is meant to be used if you have a domain as mykewlwebsite.com and have the ability to add sub-domains to it, so you just configure the home folders for each of them:
mykewlwebsite.com: path/to/mykewlwebsite.com/apps/frontend/public/
admin.mykewlwebsite.com: path/to/mykewlwebsite.com/apps/backend/public/
api.mykewlwebsite.com: path/to/mykewlwebsite.com/apps/api/public/
You have a few options here:
1) CREATE A SUBFOLDER - Put them in a folder called admin within the controllers directory (in application/contollers/admin/products.php)
A word of warning here is that you can only go one folder deep or codeigniter gets upset. Also, it will use first level controller/methods first so be careful of naming conflicts - e.g if you have an admin controller with a products method in it, that will get called before it looks in the admin directory for a products controller. (when going to example.com/admin/products)
2) USE THE ROUTES FILE - If it is just the urls you are worried about you could just call the controllers whatever you want and use the application/config/routes to redirect those paths to the controllers you want like this:
controller name: admin_products.php
routes file:
$route['admin/products'] = "admin_products";
3) USE A MASTER CONTROLLER FOR ALL - Final option would be to have a single admin controller and use named methods inside it, so for example you have admin.php controller with a products method within it this would then be called by admin/products uri (this will probably get messy though in a big application/site - not recommended)
Hope I have explained this OK for you, if you need any clarification please ask and I will try to elaborate.
I noticed Joomla, Wordpress and other CMSs have blank index.html files in ALL their sub folders to prevent people from peeking into the folder structure. My question is why can't they forbid folder viewing using the .htaccess file instead of putting a blank index.html file into all the folders. What's the difference and why have they chosen index.html?
Turning off folder 'Indexes' is best done in the master httpd.conf or vhosts.conf file rather than local .htaccess files.
Joomla (and Mambo before it) have been around quite a while and are used widely on shared hosting servers. The decision was taken to use index.html files as a safe fallback given the 'mixed' nature of shared hosting. Obviously .htaccess files are apache only, can cause server 500 errors if present on servers not expecting them or if they contain directives not supported by certain server setups. Consequently by default Joomla doesn't ship with a .htaccess file as such. There is a htaccess.txt file which the user needs to put in place manually if they activate certain features. It is assumed that a user knowledgeable enough to put the file in place will understand the consequences and if it does kill their site they will immediately understand the cause - due to the error immediately following their actions.
As server setups have advanced there is (sometimes heated) discussion about the current validity of the use of index.html files - but for now the policy is that all add-ons should ship with 'blank' index.html files in all folders.