CodeIgniter URI Routing (htaccess) with multiple Applications - php

Hello stackoverflow community,
I've got a little Problem with my CodeIgniter.
I use two applications in my app folder. The first one is the frontend, the second one the backend.
In my root directory I've two .php files. An index.php leading to the frontend directory and a backend.php leading to the backend directory.
Since I use mod rewrite in order to get clean URL's there is a problem with that.
The URL structure is the following: www.domain.com/controller/action
That's the action of the controller in my frontend Application.
I use htacces to get rid of the /index.php/ between domain and controller.
To access my backend application I want my URL to be like this www.domain.com/admin/controller/action
therefore I have this rewrite rule:
RewriteCond %{REQUEST_URI} ^admin.*
RewriteRule ^admin/(.*)$ /backend.php?/$1 [L]
Now the problem:
CodeIgniter assumes that /backend/ is the first URI segment, and wants to treat it mistakenly as my controller.
Do I really have to edit the core of CodeIgniter in order to tell it not to use the Server Request URI or is there another trick?
Thanks in advance,
Thomas

This really isn't going to work. Check out config/config.php and config/settings.php etc for constants that need setting... You really need to run each application in a separate 'application' directory. It's also worth asking yourself if they even need to be 'separate' applications...
Basically, stop going down this path while you still can, it will lead to heartbreak, and reams of horrible code.

Instead of having two application folders I would suggest having a folder within the controller called 'admin', you could also have folders within your models, views (even libraries) named 'admin' which you could then pull out of this project and move to other projects.
This would allow you to just use a standard rewrite to remove the index.php from the URL, and domain.com/admin would point to your admin folder within the controller.
You just need to be sure not to have a controller within the root controller folder named 'admin.php' or you will run into issues.
For more information take a look at: Organizing Your Controllers into Sub-folders within the CI docs.
Also note that an out of the box MVC framework might not always be best used for creating a CMS. Typically an MVC framework is best used to quickly create web applications which have fairly static routes pointing to specific controllers. CMS's on the other hand tend to give full control over the website, which leads to database driven routes which often eliminate the need for typical controllers unless you have a heavily modified routing system.

Related

Folder structure for modular development with PHP and vuejs (or angular)

Essentially, I'm looking to have a PHP development workflow that needs to be modular, but using a Single Page Application technology.
I understand it is recommended to separate the back-end from the front-end. Develop them separately. But is there a way to group all related code into one module (or folder), meaning all backend code with its own views presentation inside the same folder?
It's like MVC, but the "V" contains fragments of vuejs (or angular) files, which extends from a master file somewhere in your project.
For example
Assume we are building a modular CMS, where you can upload "plugins" (really, PHP modules), extending the CMS' functionality:
-project[root-folder]
----core[folder] # contains all infrastracture code, api routes, master view file, magic, etc.
----modules[folder] # uploadable modules goes here
--------User[folder] # sample module; follows the MVC pattern
------------Controllers[folder] # contains files, e.g. UserController.php
------------Models[folder] # contains User.php
------------views[folder] # where vue components is housed
----------------users/index.php # contains vue code
----------------users/create.php # etc...
----------------users/js/user.js
----------------users/css/user.css
--------Blog[folder] # another module
----index.php # the master view or just the bootstrap file
----gulpfile.js
Then inside the core/ folder, there is a master layout that binds all views together.
Will a folder structure like this be viable?
Obvious problem there is you can't use .vue files (as that would mean, every time you upload a new module, you need to run gulp or re-compile).
Hoping for your feedback. Thanks.
This question will strike a lot of folk as bizarre and twisted. That's the reaction I got when I asked it in the context of .net mvc. I'm with you 100%.
I'm too new to js frontend development (and too ignorant of PHP) to have much advice. It's going to be tricky. Ajax calls to PHP code will need to go to paths below the src directory. But then you want to stop your frontend resources being served from these same paths. Both PHP and gulp will want to use file paths for urls, but at least for Gulp this can be controlled.
I'll follow this with interest. My ambition is to keep in the same folder things you're likely to want to delete together, and for those things to be able to call each other with short, relative paths. The ideal would be to be able to specify the module route independently of the path on disk, and to have this route work for both frontend bundled resources and services. Good luck !
I came across this question whilst searching for an approach for exactly the same problem. I'm building a "platform" rather than an application with a plugin system along the lines of Wordpress. I have the additional issue of the platform itself being a 'multitenancy' environment, too - so any plugins cannot interfere with the core "Dashboard" that holds these things together.
So; posting for a few reasons, two years on...
Did you get anywhere and would you care to share any thoughts?
I came across a quite extensive article for PHP Phalcon that has certainly given me a few ideas. Sharing incase it helps you/others:
https://blog.antsand.com/singlepost/index/5619/How-to-integrate-php-(Phalcon)-and-Vue.js-components
There's a line buried in the series that says "As a rule of thumb. Structure your code, based on the application and NOT on the programming language and frameworks." I'm not sure how wise or not this is, but it certainly gave me something to crack on with.
So right now, I have a module folder a bit like:
/mymodule
/Controller
/Model/
/Template
thing.vue
/Assets
/js
/css
MyModule.php
Assets are handled via a framework route (i.e, /assets/{path:.*} )
Templates are handled via the (PHP) module install script to make sure webpack knows where they live.
Still at proof-of-concept stage but rightly or wrongly, it seems to work well enough!

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.

can i use multiple php frameworks for a same site?

Actually, I've sub folder named 'portfolio' (http://www.mysite.com/portfolio) & for that folder i'm using codeigniter but for the root directory of my site (http://www.mysite.com) i want to use laravel framework and angular.js ?
If you don't plany any shared functionality between the portfolio and the rest of your site (like a common login system for example), there's no problem with using two different frameworks. All you need is a .htaccess file rewriting all portfolio requests to CodeIgniter's front controller.
This is no problem, you only have to separate the requests on the webserver level so that some go to the Laravel framework, and some other go to the Codeigniter part.
Don't expect support inside the framework, though. It's a special solution for your special case. You have to know how to use rewriting URLs with your webserver.
Be careful with url-rewriting and set it up properly, especially for your main site (cancel rewriting for your /portfolio/ folder). Many sites uses Wordpress for their blogs, but another framework/script for main site.

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.

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