Better file naming conventions or structure - php

I create folders and then add files e.g.
folder: orders
files : view.php, add_update.php, invoice.php
Uri becomes: orders/view, orders/add_update, orders/invoice
another example
folder: user
files : view.php (view profile), add_update.php (add, update users)
Uri : users/view, users/add_update
and many more folders and files having similar structure. It works fine but in the editor (netbeans) I see the the name of the file only in the tab so that if I have opened order/view.php, users/view.php, files/view.php I see view.php, view.php, view.php ... I can certainly hover over and see the full path but I was wondering if you guys follow a better file naming pattern. I sometime make mistakes when uploading orders/view.php or users/view.php cos the files structure look so similar. What would you suggest?
PS: These are controllers not the CI view files. One of the reasons I have this structure is to have meaningful uri. I just tagged in CI but it doesn't matter if I am using CI or not.

Why the need for folders? Why not simply have a class of Orderswith methods view, update, etc?
Barebones example:
<?php if (! defined('BASEPATH')) exit('No direct script access');
class Orders extends CI_Controller {
function __construct() {
parent::__construct();
}
/* URL = example.com/orders */
function index()
{
/* default action */
}
/* URL = example.com/orders/view */
function view()
{
/* view method code */
}
/* etc */
}

you can rename your files like order_view.php, order_update.php etc and similarly user_view.php, user_update.php... it will differentiate the orders and users in your IDE

I would suggest working with namespaces, and give the views a more meaningful name.
This is how my Netbeans looks:
And I have assigned namespace \Ganymedes\Views to all of them. So I have meaningful names, and still everything in the same namespace.

Related

Change default controller directory of Codeigniter

I want to have two folders where save codeigniter's controllers:
/application/controllers
/application/buckets
i'm a order paranoic person and i want to separate two types of my controllers.
In bucket folders the structure app was this:
/application/buckets/example/index.php
/application/buckets/example2/index.php
/application/buckets/example3/index.php
¿Maybe extending the router class?
A working example:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
Extended the core Router class to allow for sub-sub-folders in the controllers directory.
*/
class App_Router extends CI_Router {
function __construct()
{
parent::__construct();
}
function _validate_request($segments)
{
if (count($segments) == 0)
{
return $segments;
}
if (file_exists(APPPATH.'buckets/'.$segments[0].'/index.php'))
{
$this->set_directory(APPPATH.'buckets/'.$segments[0]);
$this->set_class(ucfirst($segments[0]));
$this->set_method(isset($segments[1]) ? $segments[1] : 'index');
return $segments;
}
}
}
You can use Hierarchical MVC(HMVC) with Codeigniter to accomplish this.
For reference, see Modular Extensions - HMVC
You may want to look into parent-child controller ...one extending another. To be more clear you can make as many controller you want.
I Agreed with #Brian Gottier : "what does changing their location do?"
You can perform anything if you have core functionalities in your hands.
You can play around with hooks (CodeIgniter's Hooks feature provides a means to tap into and modify the inner workings of the framework without hacking the core files. When CodeIgniter runs it follows a specific execution process, diagramed in the Application Flow page.)
Create "Base"/"Admin"/"Public"/"XYZ" Controllers in
application/core/MY_Controller.php
and keep rest of your controllers in same application/controller folder
MY_Controller is a basic core library extension. Whenever you create a class with the MY_ prefix the CodeIgniter Loader class will load this after loading the core library.
All we have done here is create a base class that all of our Controllers and "controller types" will inherit. Anything we put in here and assign to $this will be available to anything that extends this class.
Base Controllers are a nice simple way to give you global data, logic and shared code which can be specific to a certain part of your site. They can do all sorts of crazy stuff which I will leave for you to think about.
I Hope this help.

Separating Controllers for each module in Code Igniter

P.S This question may already be existing but the answer didn't satisfied what I was trying to figure out. I'm using code igniter for the first time in my new project.
So I have this only one controller which is main.php and populated with a lot of public function. Now everytime I go to main.php the url is looks like cmms/main and everytime I'm going for its subclass it goes cmms/main/asset.
Now the subclass asset has many functions which are in main.php. What I want is to make separate controllers for each module. so i have cmms/main as main.php & cmms/asset as asset.php instead of making it under cmms/main/asset. Would this be possible? or I should just leave it alone and continue putting all codes in the main.php controller?
My default route is the main controller.
$route['default_controller'] = 'main';
You have two ways to do that.
Keep one controller but URL is different. (Method 01)
Keep new controller for each new function. (Method 02)
(these topics describe below)
Method 01
go to - config/routes.php, and add new routes like this
$route['cmms/asset'] = 'cmms/main/asset';
$route['cmms/contact'] = 'cmms/main/contact';
So in view you should call anchor tags like this
Assets
Contact Us
Method 02
Create new controller for each new methods.
File name - main.php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Main extends CI_Controller {
}
File name - asset.php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Asset extends CI_Controller {
}
File name - contact.php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Contact extends CI_Controller {
}
So in view you should call anchor tags like this
Assets
Contact Us
you can create Modules for your class in following ways,
By creating separate class under your application controllers directory eg: main.php, assets.php here your URL will be cmms/main or cmms/asset
Creating sub folders under your application controllers directory and create controller classes under it main/main.php, asset/asset.php here URL will be cmms/main/main, cmms/asset/asset, if you want just cmms/main or cmms/asset in URL than you need to create routes in route.php file.
This way you can make codeigniter Modular in this way every module will have separate controller, model and view even libraries in its own module directory and URL will be same as cmms/main or cmms/asset for each controller. you need to install wiredesignz's HMVC extension here is URL you can download https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/overview
important please read documentation for HMVC extension to use it.
it seems that you are having trouble to write long url each and every time....
So,better way is to use routes for ease.
For Ex: $route['login'] = "main/login";
Now if you use login directly in url it will call main class and it's login method
using routes you can prevent exposing your controller name and also for shorten URl

CodeIgniter MVC - Is it a good practice to have Models serve views?

I'm developing a news website using CodeIgniter 3. Almost every page contains a Notice Board content of which is same on all pages. I have created a view for this section named 'noticeboard.php'. And different pages are controlled by different Controllers. i.e. Home, Post, Gallery, Poll, etc. The problem using controller here is I need to perform two operations on every controller to generate the noticeboard HTML.
Controller looks like:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class SomeController extends CI_Controller {
/* .. */
public function index() {
/* .. */
$noticeboard['notice'] = $this->noticeboard_model->getNotice(); // Gets Notice's HTML
$data['noticeboard'] = $this->load->view( 'sidebar/noticeboard', $noticeboard, TRUE );
/* .. */
/* $data is served to main page template */
}
/* .. */
}
And views/sidebar/noticeboard.php looks like:
<div class="noticeboard">
<h3>Notice</h3>
<?php echo $notice; ?>
</div>
I could solve this code redundancy by simply having a getNoticeboardHTML() on Noticeboard model and having the model fetch the view and return HTML. But is it a good practice?
Better you create a custom library and put getNoticeboardHTML() in that class. After that call this wherever you need. Do not dirty your model with HTML.
Create Custom Library
Theoretically speaking, No. You are not supposed to do this. Its a violation of MVC. You could use a library class to avoid the code duplication.

understanding the basic codeIgniter

<?php
if (!defined('BASEPATH', ''))
exit('no direct script access allowed');
class Hello extends CI_Controller {
public function index() {
echo "this is my index function";
}
public function one() {
$this->load->view('one');
}
}
?>
I am the new user of codeIgniter.First i download code igniter software,then place it to the xampp/htdocs.Then i create a simple codeIgniter code which are show in the above.I don't understand where this code is to be save.I save this code in the Codeignitor/appilication.but it give me the below answer:
no direct script access allowed
What are the fault.please help me anyone.
Have you read the User Guide of Codeigniter? This is your Controller code and it should be placed inside appilication/controllers folder by name hello.php. See this.
Before starting of any framework it would be good if you understanding directory structure, and what is the role of files.
Where should keep HTML, CSS, JS and logic and model part.
As you creating controller then it should save under
application/controllers/ControllerName.php
Here is the guideline for controller
Also refer tutorials
http://tutorialcodeigniter.com
http://codesamplez.com/development/codeigniter-basic-tutorial
Open config.php within CodeIgniter\system\application\config.
Change base site url http://localhost/foldername

How to make a proper index page for subdirectories in CodeIgniter

I'm building an app that has a section for consumers and businesses, and I want to separate the controllers folder appropriately, so it looks like this -
http://domain.com/users/signup/
http://domain.com/business/signup/
I have it working by creating a separate folder for each section in the "controllers" folder, but I want to know how to make an appropriate page when the user visits the http://domain.com/users/. It currently just loads the homepage. How can I fix this?
You don't need to put them in separate folders for this to work.
File system/application/controllers/users.php:
<?php
class Users extends Controller {
function index() {
// this function will be called at http://domain.com/users
}
function signup() {
// this function will be called at http://domain.com/users/signup
}
}
?>
File system/application/controllers/business.php:
<?php
class Business extends Controller {
function index() {
// this function will be called at http://domain.com/business
}
function signup() {
// this function will be called at http://domain.com/business/signup
}
}
?>
If I understand your question correct, you might be looking for URI Routing. I don't see why you would need folders for this. Simply just create the routes you need and make use of the index methods in the controllers for making the "homepage" for both users and business.

Categories