Yii PHP Framework- Implementation - php

I was just curious if Yii is supposed to be compiled into PHP or not. Is it possible to use Yii just by copying the Framework to a folder on the server and then including something (something as in one of the yii files- I am not sure how it works) in the scripts I wish to use the framework for?
(Noob when it comes to frameworks and usage)
Thanks,
Josh

I'll go with the line
Is it possible to use Yii just by
copying the Framework to a folder on
the server and then including
something
and answer yes :p Though, you should just follow webapp creation through yiic webapp like
so:
Download the yii (yii-someversion.tar.gz or what have you) distribution,
extract it somewhere (e.g. /opt/yii in *nix or C:/web/yii in windows.)
Now put that directory in your path ($PATH in *nix, or %PATH% in windows),
go to a shell / command prompt, change the directory to your
webserver's document root and do a yiic webapp <app folder name>
After answering a couple config question, you should have an look at the
index.php created there, you should have something like:
<?php
// change the following paths if necessary
$yii=dirname(__FILE__).'/../yii/framework/yii.php';
$config=dirname(__FILE__).'/protected/config/main.php';
// remove the following lines when in production mode
defined('YII_DEBUG') or define('YII_DEBUG',true);
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
require_once($yii);
Yii::createWebApplication($config)->run();
and that's about it :p

Yii is a PHP framework and not an extension. You can just copy-paste it somewhere. Be sure to put the destination folder into your include path.

you just need to do it once to get an instance, and then you can copy this instance as much as you want, provided that the path of yii.php file in the index.php file is right, for me I usually copy the framework folder from the yii folder, paste it inside the instance in the folder "protected" and then change the path in index.php to "/protected/framework/yii.php"

Related

Admin function with one PHP config file

This might be hard to explain but I am looking for the best method of having one or a group of config files so if I need to update something its a little easier to do.
I have wrote a PHP application that has a sub folder for the admin side off the root folder and includes folder that is sub folder off the root folder as well .(see below)
the include folder has database config files, loads common variables and so forth. the problem is the path for the admin files that call for the database connection are obviously different than the files in the root folder.
so I started this but now I wonder if there is a better method than the route I am going.
`if($adminfile=="yes")
{
require('../includes/database/connect.db.php');
}
else{
require('includes/database/connect.db.php');
}`
I would really appreciate some advice, should I scrap this idea and have 2 location for the config file? Part of me hates to include in all the standard code $adminfile="no" I keep thinking is there a better way.
How do others solve this problem?
Check the value of your include_path in php.ini or your local config (via .htaccess for apache is another way to do it. If you add the path to demo to the include_path setting, then:
include('includes/database/connect.db.php');
or
require_once('includes/database/connect.db.php');
Will work from any file or sub folder.
Another way to do this is to include a single bootstrap file that has all the settings (i.e. not just your database ones) in your scripts.
A better way to do this is to route all your requests through a Front Controller that does anythign setup/teardown you need on every request. See PHP Front Controllers
you can define a constant in every file ... which defines the root folder you have
define('root', 'demo/');
and do
require(root.'includes/database/connect.db.php');
and this will work fine with any file you want to require

php declaring application root

I have a little problem: I began a project as a subdirectory in a larger web project. Thus the web file path is something like /../myProject. But things have progressed and I've realized that this should be its own project. However, I'd like to be able to keep it where it (as a sub-directory) also make it a sub-domain wherein myProject becomes the root. (There is also the possibility that my project will be mirrored at a library site, where it will once be in a sub-directory).
The problem I having with all this is that in some cases I have html_partial files, (for instance for the header or footer). But the relative path of these partials differs depending on where you are in the file tree. I originally solved this by always going back to the root.
But now, you see, depending on where my project lives, the root will be different. What I'd like to do is declare myProject as the "application root" and then be able to use relative paths based on this application root rather the than the web root'. This way, all of the relative paths within 'myProject' will work no matter wheremyProject` lives in the web path.
Does PHP have a way to declare something like an Application Root if so, can you explain it me or direct me to its documentation. Thanks!
You could simply have a PHP file in your application root directory which would define the directory it is in as the application root. The file could be as simple as this:
<?php
define('APPLICATION_ROOT', __DIR__);
?>
You could then include this file as needed and base all of your file paths off of APPLICATION_ROOT. Note that APPLICATION_ROOT would not have a trailing slash as defined here (unless your file happened to be on in the machines root directory, which is unlikely).
I usually do something lile this in the front controller:
define('APPLICATION_PATH', realpath(__DIR__));
Then you can do things like:
set_include_path(APPLICATION_PATH . '/include');
Or:
$fp = fopen(APPLICATION_PATH . '/path/to/some/file', 'r');
If your app doesn't make use of a front controller, you could define an environment variable in your vhost config or .htaccess:
SetEnv APPLICATION_PATH /full/path/to/my/app
And then use:
getenv('APPLICATION_PATH')

PHP: Find out variable value with minimal evaluation

I'm writing a plugin for my editor to support the Yii framework.
To make this plugin work I need to find out 2 paths from index.php: the
framework and configuration paths.
The usual index.php (which is the entry point for Yii) looks like this:
<?php
// change the following paths if necessary
$yii=dirname(__FILE__).'/../../../../../usr/share/php-libs/framework/yii.php';
$config=dirname(__FILE__).'/protected/config/main.php';
// remove the following lines when in production mode
defined('YII_DEBUG') or define('YII_DEBUG',true);
// specify how many levels of call stack should be shown in each log message
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
require_once($yii);
Yii::createWebApplication($config)->run();
Basically I need to get list of required files and arguments for Yii::createWebApplication().
But the code here can be anything. Literally anything. So is it possible to run this file somehow up to Yii::createWebApplication() so I could find out where the application directory is and where the framework is?
You can use Yii::app()->basePath to get the root path of the application, and Yii::app()->baseUrl to get the relative URL for the application.
The Yii framework must to be in root htdocs of the Apache.

How to declare paths in PHP to allow easy app move to not rooted folder?

I have wamp setup on my windows box. Generally, when I bring a site down from the web, I create a folder inside my www folder for the site name. ex: c:\wamp\www\mysite. Once I have the folder, I copy down all the live files. The issue is that all the paths are then broken because my local folder isn't rooted.
What is the best way to setup paths so that if the site moves to a folder that isn't rooted, it will work easily?
I use a file (usually called something like config.php) to keep track of the root folder. My definitions (constants) look like this:
define('BASE_DIR','/wherever/whenever/');
define('LIB_DIR', BASE_DIR . 'lib/');
And then when you need to include a file
include LIB_DIR . 'aFile.php';
This would be something you do on a new site or if you have time to refactor your current site.
Create an include file, that has constants setup based upon whatever the root directory is... then in your code, use the constants you created to include files.
Also note, that when you are using directory "slashes", always use the build in constant DIRECTORY_SEPARATOR instead of hard coding it, this will allow you to go from WIndows to Linux seamlessly.
We use $_SERVER['DOCUMENT_ROOT'] to determine where we are in the filesystem and then simply append the folder name of our project to that. This works perfectly for us. You should always use a configuration.php where you define basic paths and URL's that may change when moving the project from one server/folder to another.
Option 1. Use <base href=""/> tag
Option 2. Use a config file, like #MattCan suggests
Option 3. Use a server environment variable, like #Bjorn suggests
Option 4. Create a virtual host on your apache, than you can create a domain who appoint exactly where are your app folder. Apache Doc here

OpenId library (include files are not found by script)

I have been trying to implement openid functionality into my website. I downloaded the JanRain's library.
I extracted the 'Auth' folder in my classes directory and following the example in the 'example' folder I created the try_auth.php, finish_auth.php, common.php file in the include directory.
Now when I click on the openid selector link I am presented with an error message that says 'openid.php' file not found.
This file is present in the Auth directory.
I corrected it and then I am being presented with a different error which says 'Auth/Yadis/HTTPFetcher.php' not found.
If I sit and change the require path individually in every file in the auth folder then it will take a long time.
my apps directory structure is like this
app
classes
Auth (openid library)
config
elements
includes
views
webroot
index.php
Please help me what am I doing wrong. How do I set the includepath so that all the files take their respective paths automatically.
Thanks
as the documentation states (you don't mention a version, so i am assuming you are using 2.x.x), the Auth/ directory in this package has to be in your PHP include path. there are various ways to do that: php.ini, httpd.conf/.htaccess, ini_set(), ... if you do it in your php.ini, with your apps directory being /path/to/your/app, it would look like that:
; UNIX: "/path1:/path2"
include_path = ".:/php/includes:/path/to/your/app/classes"
;
; Windows: "\path1;\path2" or "c:/path1;c:/path2"
;include_path = ".;c:/php/includes;c:/path/to/your/app/classes"
The files are there you're just not setting the path correctly.
You said this is the path it's looking for 'Auth/Yadis/HTTPFetcher.php'
You might need to add the full path, something like this:
/var/www/html/whaterver/Auth/Yadis/HTTPFetcher.php
or
/this/is/where/you/put/the/path/to/the/file/Auth/Yadis/HTTPFetcher.php
just do this command to find the base path and append it to your file path
echo `pwd`;
NOTE: those are backticks not single quotes around the pwd command
EDIT:
You just need to add this to the file that your trying to include into your script.
EXAMPLE:
your file is here: /var/www/html/index.php
and you need to include this file here: /classes/package/files.php
This file: /classes/package/files.php know where all the other files are that come in the package, so no need to edit any of these.
But you do need to edit the /var/www/html/index.php file and add something lie this:
include('/var/www/html/classes/packages/files.php');
once you have this in your script it should know where everything else is.
or as #ax has stated this looks to be a php.ini configuration
Hope this helps
You can set the include path with the set_include_path function call (http://php.net/set_include_path), if that's what you're asking...
Do a getcwd to find the directory you are in and make the appropiate chdir(s) to resolve your problem. It is a dirty solution but it should work with a minimal effort.

Categories