Codeigniter HMVC modules and javascript files - php

I've just started learning about HMVC in CodeIgniter.
So far I've been enjoying having modular controllers, but problems come when I wish to include javascript.
It seems to me that I'll have to include javascript file from the view instead of the widgets which isn't really good because I tend to forget which widgets has to come with which javascript file.
Anyone has a better way to do it?

Assets (css,js,images) you should place outside of application folder so you can access them directly.
Thus , you load them using base_url() to start with, and base_url() remains same from wherever you call it.
if you want to split assets in modules as well, perhaps make an assets folder, which further contains folders with module names, each containing css,js,images files. then use base_url()."/assets/module_name/js/script.js"
or something of the sort

As default, you cannot access files within your module folder as it was protected by .htaccess in applications folder.
To allow access in your assets/public folder within your module folder just add another .htaccess within the folder and add the following line.
Allow From All

Related

CSS include path messed up with vhost

I am currently using SLIM as a PHP framework. I have a slight problem with paths (below is my file hierarchy).
I'd like to be able to have a clean url that presents itself like this: "https://cppoi/home", but by doing this my html includes no longer work as they are supposed to. It's as if they can't see outside their "bubble" or the vhost bubble, which points to "application/". If I however set it to point to the entire project folder, which includes the assets folder in which my stylesheets are located, my includes work again but my url becomes "https://cppoi/application/home" which is uglier.
There is probably a very easy solution to this, thanks for your time.
There is no super easy solution, your app setup doesn't necessarily follow best practices. You would usually setup a public folder that contains only your front controller index.php and any public assets. (+ stuff like .htaccess. This public folder is then exposed via webserver, nothing else. That way your code is not accessible from the web, but your main entry point (front controller) and your assets are.

Absolute file path in PHP for linking assets

I have been setting up a simple framework for myself to use for projects with PHP.
My basic folder structure is currently as follows from the root:
root folder
package.json
gruntfile.js
node_modules
src
index.php
pages
about.php
contact.php
partials
header.php
footer.php
js
css
Because I'm storing all of my pages in their own directory but the index.php just outside of the pages directory, what I am essentially looking for is a way to get the absolute path to the index.php that I can then store in a variable and call for any links such as the stylesheet or javascripts within the site.
So my question basically is just how to do that. I am currently using MAMP as my localhost environment, feel free to ask me about any other information regarding my question that I might have missed.
From the look of it, your index.php would be also where you set your Apache DocumentRoot?
If I understood correctly, you can use PHP's $_SERVER variable and the one you would be interest in is $_SERVER["DOCUMENT_ROOT"]
On the other hand, you mentioned you wanted to use this variable for CSS and Javascript files? From a client side it would be impossible to use an absolute file path but you would rather need a relative path (because the client aka browser, have no visibility on your web server's file system).
If you are serious about this framework, I would also suggest looking into design patterns such as MVC. And also consider implementing a FrontController to dispatch all your requests. This would give you more control on paths and how you parse your files.

Phalcon custom assets routing within Module

Using Phalcon Assets Management. I'm able to add assets like JS, CSS file into volt template from the controller.
But the assets manager will always looks inside "public" folder for assets.
How can we access "assets" folder which is inside "module" directory?
So that each module will have there own assets defined.
Here's my current folder structure:
---apps
------module_1
---------controllers
---------models
---------views
------------assets // how to access this to get JS, CSS, Img files into Volt Template?
---public
------assets // currently all assets are accessed by this folder by default
The Assets Manager is just a like View Helper to your code be more future proof.
I think you're trying to apply the MVC pattern on the server and client side at once, and that's not going to work. I mean, the server/client side segregation comes first than MVC itself.
I admit that the MVC Pattern gets a bit confusing on the client-side of a Web Application, but that's another history. Simply remain with everything that's isn't processed by the server in your public folder and you'll be fine. You can even create one folder for each module like you wanted in the 'public/assets' path.

Yii's asseets folder with no write access

I am trying to deploy a PHP Yii app to Orchestra (https://www.engineyard.com/products/orchestra/). The platform, like I think many cloud-based platform, doesn't allow write permissions.
I've managed to get around the 'runtime' directory that Yii requires by putting it in the system's tmp folder. However I'm stuck with the 'assets' folder. Yii requires a writable AND publicly accessible folder.
Is there a way around this?
Yii requires somewhere to put the files from within the core or modules to be publicly accessible.
If this isn't possible you might have to go through and manually grab each js/css file your going to want, place them in the folders required and use scriptMap to map these back or block them and include them yourself.
There's lots of documentation around Client Script which is what handles all this.

Building a CMS to For Website

I have my main site kansasoutlawwrestling.com which will be using Codeigniter, and then I am also creating a CMS for myself that is a separate entity which will be located at kansasoutlawwrestling.com/kowmanager.
My CMS will use different CSS, javascript, and image files, so I'm wondering if I should just have two different installs of CI. I tried looking at PyroCMS, but there's way too many folders and I was having a problem understanding its file structure. What is the proper set up for this is?
The basic structure of Codeigniter is that you have 2 folders and 1 file in your root folder:
root/application/
root/system/
root/index.php
Now, obviously, you might have many more files and folders in there as well, but these are the basics upon which every Codeigniter app runs.
What do each of these do? To begin with, every page request starts at index.php. This page set's up some configurations and some constants, and then hands over the reigns to Codeigniter.
Where is "Codeigniter" located? That would be the system folder. This folder should never be touched, by you or anyone else. Everything pertaining to your app is stored within the application folder. This includes all your configurations, your controllers, your models, your views, even your library extensions (although you could store other stuff outside this folder, like images/css/js/fonts etc.).
So, the correct way to set up shop would be:
root/application/
root/system/
root/index.php
root/kowmanager/application
root/kowmanager/index.php
But, you have to inform your kowmanager's index.php that the system folder is not located in the same directory. So, within the index.php (inside of kowmanager), at around line 25, you should see this:
$system_path = "system";
Simply change it to:
$system_path = "../system";
and you're done.
Now both your apps (your main site and you CMS) will be sharing the same Codeigniter base. When the time comes to update CI, you'll do that once within the main system folder...
I've done several Codeigniter CMS's and taken both routes:
Integrated (shared application files and assets)
Separate installation (only shared system files, if any)
At first I liked the convenience of the integrated approach: when I needed a custom library or icon file for the front and back end, it was available without duplication. I've since changed my mind.
My opinion now, after 4 years or so of working on these, is that the benefits of having an integrated CMS is short-lived.
90% of the code is in the back end, so you end up with lots of helpers, libraries, etc. that are only used for administration.
Any shared resources that you need to tweak can end up working great on one side, but breaking the other, or being overkill/useless.
Models tend to be bloated for use on the front-end when they are full of code that's only used for the back end.
Shared templates, js, and css files almost never work. The control panel probably doesn't need to work in IE{insert version here}, but your front end should.
It makes updates and upgrades to either end sketchy, unless you know exactly what you need to update and what not to touch, and where you may have made customizations for a particular site's front end that should not be altered.
Auth logic is much easier when your admins and regular users aren't in the same bucket
Separate installations are easier to set up, and they can be "tacked on" to an existing site rather than having to integrate it.
My advice: Go with a separate installation.
If I were you, I would probably not go the separate applications path. If you're sharing things like code that renders a page or logs a user in, you'll be repeating it for both installs. Obviously two separate installs would only require one system folder of which you'd share as nothing changes in system. If it were me, I'd probably just set up a route in your config/routes.php file.
Something like the following (presuming you have a controller called 'kowmanager' inside a folder called 'kowmanager' in your controllers folder):
// This would redirect all calls to kansasoutlawwrestling.com/kowmanager
// to the kowmanager controller.
$route['kowmanager'] = "kowmanager/kowmanager";
// Redirects all kowmanager/method requests to the kowmanager folder
// and a particular controller
$route['kowmanager/(:any)'] = "kowmanager/$1";
// Redirects all kowmanager/method requests to the kowmanager folder and a
// particular controller and method inside controller.
$route['kowmanager/(:any)/(:any)'] = "kowmanager/$1/$2";
Might not be the best option, but it means you won't repeat the same code twice and you've essentially created two applications inside one. There are numerous other ways of doing this including some rewrites in your .htaccess file.
If you want the easier option, go separate installs and be mindful of code repetition. Stick to the DRY (Don't Repeat Yourself) methodology.

Categories