To change the file extension in NetBeans, right-click the file and choose Properties. In the Properties window, under Extension, change the value, and hit Close.
Friday, March 11, 2011
How to change a file extension in NetBeans
I had a .html file that I wanted to change to .php, but NetBeans doesn't let you change the extension with the Rename or Refactor menu option.
Wednesday, March 9, 2011
Yii: How to set a value in the parent layout from a child view
Say you have a search box at the top of your page, but you want to make it contextual to the area that you're in. For example, on the contacts view, you want the search button to say "Search Contacts". Your search button is on the layouts/main.php page. Here's how:
open protected/components/Controller.php
add a variable, such as "searchButtonArea"
in layouts/main.php, add that value to the search buttons, such as <?="Search $this->searchButtonArea"?>
then, in your contact views, set $this->searchButtonArea = "Contacts";
Or, add a property to the ContactsController, $searchButtonArea='Contacts';
open protected/components/Controller.php
add a variable, such as "searchButtonArea"
in layouts/main.php, add that value to the search buttons, such as <?="Search $this->searchButtonArea"?>
then, in your contact views, set $this->searchButtonArea = "Contacts";
Or, add a property to the ContactsController, $searchButtonArea='Contacts';
Saturday, March 5, 2011
How to get debug information about your Yii application quickly
<?php var_dump(Yii::app()); phpinfo(); ?>will tell you a lot about your current Yii app. This will show your database connection passwords, so be careful.
How to make a mobile theme in Yii
Making a mobile theme in Yii is pretty easy. Getting it running smoothly is more work. Here's how:
1. under /themes/ create a folder "mobile"
2. copy main.php from /protected/views/layouts/ to the /themes/mobile/views/layouts/ folder. You don't need to copy anything else. Views, etc will be inherited.
3. The blueprint CSS framework doesn't seem to be too mobile friendly. By default, there will be a lot of horizontal scrolling. So, in /themes/mobile/views/layouts/main.php, remove these lines:
<!-- blueprint CSS framework -->
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/screen.css" media="screen, projection" />
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/print.css" media="print" />
<!--[if lt IE 8]>
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/ie.css" media="screen, projection" />
<![endif]-->
You can test it out by setting 'theme'=>'mobile' in /protected/config/main.php
Now, you want to detect the browser type and set the right theme (which i'll cover next), BUT you also want to give users a choice. For example, you should be able to test the mobile site on your desktop web browser, without changing the user-agent string.
* one thing you may not know is that Yii looks for the classic theme on start, which is empty. But you can put views or layouts there and they will be used without configuration change.
You can add some styling to the mobile theme to make it look better. Here are some items to consider:
<style>
input[type=text], input[type=password], textarea {max-width:260px;}
h1,h2,h3,h4,h5,h6 {font-weight:normal;}
html, body, #content {margin:0; padding:0;}
#content h1, #logo, #footer, .portlet-title {display:none;}
#sidebar {padding:0; margin:0; margin-top:5px;}
* {float:none; }
</style>
So, do this. In /protected/config/main.php, set
To switch themes, make a new controller. In /protected/controllers/ create BrowserController.php, and add this text:
To switch to the mobile theme, go to webroot/browser/mobile and to switch back go to webroot/browser/anythingelse
Making the Theme
1. under /themes/ create a folder "mobile"
2. copy main.php from /protected/views/layouts/ to the /themes/mobile/views/layouts/ folder. You don't need to copy anything else. Views, etc will be inherited.
3. The blueprint CSS framework doesn't seem to be too mobile friendly. By default, there will be a lot of horizontal scrolling. So, in /themes/mobile/views/layouts/main.php, remove these lines:
<!-- blueprint CSS framework -->
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/screen.css" media="screen, projection" />
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/print.css" media="print" />
<!--[if lt IE 8]>
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/ie.css" media="screen, projection" />
<![endif]-->
You can test it out by setting 'theme'=>'mobile' in /protected/config/main.php
Now, you want to detect the browser type and set the right theme (which i'll cover next), BUT you also want to give users a choice. For example, you should be able to test the mobile site on your desktop web browser, without changing the user-agent string.
* one thing you may not know is that Yii looks for the classic theme on start, which is empty. But you can put views or layouts there and they will be used without configuration change.
You can add some styling to the mobile theme to make it look better. Here are some items to consider:
<style>
input[type=text], input[type=password], textarea {max-width:260px;}
h1,h2,h3,h4,h5,h6 {font-weight:normal;}
html, body, #content {margin:0; padding:0;}
#content h1, #logo, #footer, .portlet-title {display:none;}
#sidebar {padding:0; margin:0; margin-top:5px;}
* {float:none; }
</style>
Wiring up the Theme
So, do this. In /protected/config/main.php, set
'onBeginRequest'=>array('MyClass', 'BeginRequest'),
in /protected/components/ create a file MyClass.php like this:class MyClass
{
public function BeginRequest(CEvent $event)
{
Yii::app()->theme = Yii::app()->session['theme'];
}
}
This will look for a theme name in the session. If it does not exist, Yii will use no theme (it will look like it does normally).To switch themes, make a new controller. In /protected/controllers/ create BrowserController.php, and add this text:
class BrowserController extends Controller
{
public function actionIndex($theme)
{
Yii::app()->session['theme'] = $theme;
$referrer = Yii::app()->request->urlReferrer ? Yii::app()->request->urlReferrer : "/mob"; //or whatever your website root is
Yii::app()->request->redirect($referrer);
}
}
The above will take a theme name as a querystring parameter and put it in session, and redirect to either the HTTP Referrer or to the root of your site. You could also look for this directly in BeginRequestTo switch to the mobile theme, go to webroot/browser/mobile and to switch back go to webroot/browser/anythingelse
Labels:
android,
blackberry,
cascading style sheets,
css,
iphone,
php,
theme,
yii
Wednesday, February 23, 2011
How to send a Yii log using GMail
PHP's mail() function does not support authentication, so you cannot use it to send emails using GMail's SMTP servers.
If you want to log Yii errors to an email account, Yii uses mail() by default, so you have to override this. Here's how:
Download and install PHP Mailer from http://phpmailer.worxware.com/. Put the PHP files in your protected/components folder
Create a new file in your protected/components folder called CPhpMailerLogRoute.php, with the following code:
array('class'=>'CPhpMailerLogRoute', 'levels'=>'error', 'emails'=>'user@example.com')
There you have it. Now you can send Yii errors via your GMail account.
If you want to log Yii errors to an email account, Yii uses mail() by default, so you have to override this. Here's how:
Download and install PHP Mailer from http://phpmailer.worxware.com/. Put the PHP files in your protected/components folder
Create a new file in your protected/components folder called CPhpMailerLogRoute.php, with the following code:
class CPhpMailerLogRoute extends CEmailLogRoute
{
protected function sendEmail($email, $subject, $message)
{
$mail = new phpmailer();
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->Username = "your gmail username";
$mail->Password = "your gmail password"; //best to keep this in your config file
$mail->Subject = $subject;
$mail->Body = $message;
$mail->addAddress($email);
$mail->send();
}
}
and then, in your configuration file [protected/config/main.php], in the 'components'=>'log'=>'routes' section, add the following:array('class'=>'CPhpMailerLogRoute', 'levels'=>'error', 'emails'=>'user@example.com')
There you have it. Now you can send Yii errors via your GMail account.
Thursday, February 10, 2011
Simple HTML Dom is nice
Recently, I had to read a web page, make some minor changes to it, and save the results. I started with DOMDocument, but couldn't figure out how to write to a DOMNode.
Then I found Simple HTML DOM, and it's a nice piece of software.
It's as easy as this:
Then I found Simple HTML DOM, and it's a nice piece of software.
It's as easy as this:
$html = file_get_html($url); $someNode = $html->find($someXpath); $someNode->innertext = "Hello, World!"; echo $html->save();That's it!
Tuesday, January 25, 2011
How to run a cron job with Yii
Sometimes you need to run code on a timer, such as sending out email reminders based on database tables. This is how to do it.
1. In /protected/commands, add a file GoCommand.php with this content:
You can use your models and components and most parts of the Yii framework, but web application stuff won't work, like sessions.
4. In Windows, you can use Task Scheduler to run the PHP file using the command line. Here's an article on how to do it:
http://www.devx.com/DevX/Article/39900/1763/page/3
1. In /protected/commands, add a file GoCommand.php with this content:
<?php
class GoCommand extends CConsoleCommand
{
public function run($args)
{
echo 'Hello, world';
}
}
?>
2. If you want to use your models, components and db connection, in /protected/config/console.php, copy over your 'import' and 'components' arrays from your main.config:<?php
// This is the configuration for yiic console application.
return array(
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
'name'=>'My Console Application',
// autoloading model and component classes
'import'=>array(
'application.models.*',
'application.components.*',
),
// application components
'components'=>array(
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=my_db',
'emulatePrepare' => true,
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'tablePrefix'=>'tbl_',
),
),
);3. then using the command prompt (cmd.exe in Windows), change directories to the protected folder of your web app, and typeyiic Goand it should run your command.
You can use your models and components and most parts of the Yii framework, but web application stuff won't work, like sessions.
4. In Windows, you can use Task Scheduler to run the PHP file using the command line. Here's an article on how to do it:
http://www.devx.com/DevX/Article/39900/1763/page/3
Subscribe to:
Posts (Atom)