Controllers sub-folders inside sub-folders and routes - php

In Codeignitor application
Controllers/folder/sub-folder/sub-folder/my_controller.php
if i would like to access my_controller.php i can't do this. for that i am sure i need change routes.php file to do this.
$route['default_controller'] = "folder";
$route['folder/sub-folder/sub-folder'] = "folder/sub-folder/sub-folder/my_controller";
but is showing 404 error.
so my question is simple in controller folder how someone can use folder inside folders.
like this:
Controllers/folder/sub-folder/sub-folder/my_controller.php
and what is needed to do with routes.php, so i will not get 404 error.

I believe there is a link in the official forms talking about this. If I remember correctly CI 1.7 did not even support sub directories and was included recently and I think they still consider it a bug.
http://codeigniter.com/forums/viewthread/190563/
If you only have a few sub directories you can also put rules in your URI routing
http://codeigniter.com/user_guide/general/routing.html
Either will be acceptable so you can choose which one will help you the most or which will get done the fastest. The other option is looking into why you need so many directories, especially for the controllers because that will make your urls longer which could ding you on SEO and other factors.

Related

What are the best practices for building a simple PHP website in this structure?

I'm building a PHP powered website in this structure:
/public
/js
/css
/img
/index.php
.htaccess
/site
/inc
_header.php
_footer.php
.
.
/func
_base.php
.htaccess
.
.
So a public dir and source (site) dir with all calls routed to index.php, That's the structure, but I am a newbie and I find trouble getting started. All pages will be passed to the index as $_GET request and the appropriate layout/file will be included. but how to go with if it's like index.php?products=prod&page=info
For templating I'll use plain/vanilla PHP like so:
product-name.php:
$product = array(...);
include_once INC_DIR."products.tpl.php"
This is a learning project but I intent to make a personal website using this approach.
Sorry if it's appears to be a vague question, I'm new in PHP and English is not my native Lango.
Cheers!
This may be too broad as there's no definitive answer - however you're asking earnestly for advice so I'll make this a community wiki answer that anyone can improve upon (or kill off as they see fit).
I think you're off to a good start but there are a few things to consider:
Keeping the http files separate from application files is a good idea but I'd split it one further in future, instead of:
/public
Split that into:
/http
/https
Giving you two document roots, one for http documents and the other for https. That way, in future, if you need to add an SSL Certificate you can keep the secure part completely separate from the non-secure part. This means if you put a "Contact Us" form (for instance) under /https it can never be accessed over http - http://www.example.com/contact simply won't work (it doesn't exist under that docroot).
If you add a CMS that could also have it's own document root so that you can completely lock it down (e.g. restrict access by IP address) and that should also have an SSL Cert.
The structure of your /site directory is entirely down to you but it might be worth looking at the MVC pattern. Essentially this is a way of separating concerns, Model, View and Controller. A very simplified explaination would be:
Models are your things - it's really an entire layer that holds your classes and how they talk to the database. You might have a Product class that holds the structure of a product, with an associated Product/database class that does the fetching and updating of that product in the database.
Views are your templates - essentially how you display things on the screen.
Controllers are the glue that stick everything together - so a product category controller would know to fetch the Category model with the category id (from $_GET) which would propagate itself with the category intro from the database and all the relevant products (which would have propagated themselves from the database). The Controller would then attach the Category View to generate what you see on the screen.
With this in mind, it's likely your /site folder might look more like:
/site
App.php // core application class (eq base.php)
/model
/category
Category.php
/db
Category.db.php
/product
Product.php
/db
Product.db.php
/user
User.php
/db
User.db.php
/controller
IndexController.php // for the homepage perhaps
CategoryController.php // for a product category
ProductController.php // for a product
/view
Index.phtml
Category.phtml
Product.phtml
/sub
header.phtml
footer.phtml
index.php
The index file now simply becomes a kind of router - you pass it variables and it fetches the relevant Controller which performs the actions required.
Essentially it will be a very sparse file, it could literally be something as simple as:
require_once realpath("path/to/App.php");
$app = App::start(); // using a Singleton Pattern
$app->fetch($_GET)->content();
Singleton Pattern: Creating the Singleton design pattern in PHP5
Most MVC systems use something like Apache's mod_rewrite to route all requests to the index page. This allows you to use RESTful URLs like http://www.example.com/toys/dinosaurs - your Controller then fetches the data relevant to /toys/dinosaurs; it could be mapped to a category using an url table in the database for instance.
This is essentially how most Frameworks work in PHP but you might be interested in looking at some for inspiration, education or to use one on this project:
Phalcon : https://phalconphp.com/en/
Symfony : https://symfony.com/
Laravel : http://laravel.com/
Yii : http://www.yiiframework.com/
... and there are oh, so many others ...

Configure multiple websites using one core (CakePHP)

I have a system runing on my webserver in /my_system folder
I need to duplicate it, for 2 new websites. They'll use the SAME system, but different databases.
I need to place them in specific folders.
Like:
/my_system/client_1
/my_system/client_2
/my_system/client_3
Then I can use url prefix to guess which client I want and which database I need.
Tthe configuration was very easy, by using .htaccess and just editing some lines in database.php
But now I'm having a problem:
When I try to access /my_system/client_1/Page/1, instead of search in Pages controller, Cake tries to search the client_1 controller, which doesn't exists.
I've tried to add the client_1, client_2, client_3 to routes prefix (Configure::write('Routing.prefixes', array('client_1', 'client_2'));), but didn't worked.
So, what's the best way to work with it?
BY only using Rewrite in htaccess I can deal with it?
Obs.:
If I try to access /my_system/Page/1 I can access it normally, the only thing I need to do is work with the mentioned prefixes.
Thanks.
You'd deal with this using CakePHP's routes.
They wouldn't be prefixes because you already have a prefix of 'my_system'.
Take a look at this great yet simple article
CAKEPHP 3 – DEFINING THE CORE OUTSIDE WEBROOT
You might be able to do the same with version 2.x

Display all current URL's in yii project

I have inherited a PHP YII project and I'm trying to figure out what it has and does. I was wondering if it's possible to display all URLs that the project routes to.
Examples would be
index.php
<controller.php>/index
<controller.php>/search
<controller.php>/add
For all controllers and all views.
You must look to configs of UrlManager/rules. Here describes all rules for process requests. Than you will know what controllers and modules (even their actions) are using. Another way I don't know

Organize controllers in sub-folders

How to organize controllers under /app/controllers in sub-
folders in CakePHP? I want to create a folder like admin inside the controllers folder and I want to create some controller related to admin. If it is possible, then how can i call a controller from a sub folder?
You can use App::build() to let CakePHP know for additional packages/configurations.
App::build(array(
'Controller' => array('/path/to/controllers', '/next/path/to/controllers')
));
You need to re-think your application structure. Cake has something built in called prefix routing that you should probably be using.
This is also available in 1.x
You can't alter the CakePHP file structure "just like that". It would require serious modification of the core to achieve this, but there is almost never a good reason to do so. If you properly follow the naming conventions, everything should be easy to locate.
What you could do (that is still following conventions and comes close to what you're looking for) is create a plugin for all your admin related tasks and then you can put all that logic under app/Plugin/plugin_name/Controller instead. That way it has it's own place, although you will need to load the plugin from you main application for this to work.

What's The Proper Way To Set The Public HTML Folder Using Yii Boilerplate?

I can't find the documentation specifying how to set the public html directory. I am trying to keep all other files and folders in the site root. Since there is a frontend and backend public html folder, I don't see how this is possible.
My Site:
/site/perl/
/site/www/ <-How do you get just the 2 public folders here?
/site/.ssh
Yii Boilerplate:
runpostdeploy
yiic
/backend/
- /config
- /www/index.php
/frontend/
- /config
- /www/index.php
/common/
/console/
/tests/
Is the whole boilerplate project supposed to reside in the sites public html folder? I thought it was bad practice to do this.
*** U P D A T E ***
Spent most the day trying to figure out this problem. The closest thing I was able to find, was a comment on one of the Yii Framwork forum posts. For now, I'm running Boilerplate with subdomains like they recommend in this comment.
http://www.yiiframework.com/wiki/155/the-directory-structure-of-the-yii-project-site/#c9444
I'm not sure if this is the correct way. But it works.
You should set your site public director to frontend/www/. The idea behind Yii Boilerplate backend/www is to have separate domain for admin options (if needed), instead of having separate module. This way you'd be able to easily have separate CWebUser, Yii::app() etc. By the docs:
backend: the backend application which will be mainly used by site administrators to manage the whole system (avoiding admin modules at frontend application to avoid confusion)
I believe if you have two application, then you need two address.
You can do with sub-dir like this:
www.site-name.com -> ../frontend/www/index.php
www.site-name.com/admin -> ../backend/www/index.php
Or you can use subdomain like this:
www.site-name.com -> ../frontend/www/index.php
admin.site-name.com -> ../backend/www/index.php
It's like Wordpress, if you need enter admin page, you access through address www.site-name.com/wp-admin
But, be sure you don't use the 'admin' path in your frontend if you use this alias to backend.
Spent most the day trying to figure out this problem. The closest thing I was able to find, was a comment on one of the Yii Framwork forum posts. For now, I'm running Boilerplate with subdomains like they recommend in this comment.
http://www.yiiframework.com/wiki/155/the-directory-structure-of-the-yii-project-site/#c9444
I'm not sure if this is the correct way. But it works.

Categories