Archive

Posts Tagged ‘PHP’

Magento multi-site install: Run a specific store view via the domain.

August 9th, 2010

Now ofcourse magento comes with a website or a language switcher built in, so if you have multiple store views for a single website/store, you have a nifty drop-down list on the front-end to let the user switch to a preferred language and locale. But what it doesn’t come out of the box with is the ability to direct the user to a store view based on the domain (Most big companies own most domain extensions on their main domain.). So in-case you are trying to accomplish the above you can use this technique that I came up.

In this example I assume that you own domains for the different countries for the same website. So say www.domain.com is primary domain in English and you also own www.domain.de for German, www.domain.nl for Dutch and www.domain.fr for French and so on.

So here’s how to do it.

Pre-requisite:
All the domains mentioned above are directed to the same magento instance via apache site setup. Usually virtual hosts.

Step 1: Setup Store codes in the Magento Admin.
This can be done in the admin under System -> Manage Stores.

For this example my stores are setup using the magento admin where the store view code is the domain extension, meaning for the German store the code = “de”, for the french store => “fr”, for the dutch store => “nl” and so on.

Step 2: Changes to the main index.php file.
Add the following code at the end of the main index.php file (Just paste over the Mage::run() statement, make sure you remove this statement.).


/**
* find the domain tld.
*/
function find_tld($url)
{
$purl = parse_url($url);
$host = $purl['host'];
preg_match_all(”/([a-zA-Z0-9_-]+).([a-zA-Z0-9.-]{2,8})$/”, $host,$matches);
return $matches[0][0];
}

//Store Switcher based on URL
$host = find_tld(”http://”.$_SERVER['HTTP_HOST']);
switch ($host)
{
case ‘domain.nl’:
$store = ‘nl’;
break;
case ‘domain.fr’:
$store = ‘fr’;
break;
case ‘domain.de’:
$store = ‘de’;
break;
default: $store = ”;
}

//Set the store
Mage::run($store);

That should do it.

You can also use this technique where you don’t want to present the user with the options to switch stores for any number of reasons e.g. you have different pricing on different store views.

You may also use this technique for sub-domains, you will just have to tweak the find TLD function to find subdomain.

admin Magento, PHP, Programming, Technologies , , ,

How to retrieve Magento base URL paths from views inside template.

August 5th, 2010

Magento front controller comes with built in methods to retrieve all base URL paths that are configured via the Magento admin.

All of these methods are inside the Mage class but just like many other things aren’t mentioned openly in their forums or documentation.

So I thought I would share this with everyone.


Javascript Base URL: http://[yourswebsite]/js/
<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS); ?>


Base URL i.e. http://[yourswebsite]/index.php/
<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK); ?>
or
<?php echo Mage::getBaseUrl(); ?>


Media URL: http://[yourwebsite]/media/
<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA); ?>


Skin URL: http://[yourwebsite]/skin/frontend/[yourtheme]/[yourtemplate]

<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN); ?>


Base URL: http://[yourwebsite]/
<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB); ?>

All these are store specific, in case you are running multiple stores in your single instance of magento i.e. Mage::Run([store_code]) in your base index.php file.

admin Magento, PHP, Programming , , ,

Difference between php echo and print

June 25th, 2010

So I have been asked this question < ? php echo "a" , " million" , " times" , " over" ? > so I thought I would answer it on my wiki.

Although both these functions or more accurate known as language constructs (which is why you can use these without parenthesis() ) are pretty much the same, they have 2 key differences.

1. echo() can take multiple arguments (as mentioned above) so you don’t have to perform string concatenation on the variable(s) before passing in the value(s). Which is also a reason why echo is a little faster than print().

2. The other difference is that echo returns a void value or doesn’t return a value as apposed to print() returning a int value. This can be considered as a performance benefit. This feature also allows you to pass print() as a parameter to a echo command.

Although I thing to note, which can help you decide which one to use is that php by default uses echo when you use < ? = $var ? > syntax. Which probably means that echo is a better choice

admin PHP, Programming , , ,

.NET V. PHP: Top 10 .NET Myths Exposed

June 16th, 2009

Here’s a lesson for anyone in advertising - confuse your customers. It’s a dream ticket, as Microsoft have demonstrated with their marketing of .NET. You can almost hear Google straining under the weight of “What is .NET?” queries. It’s almost as cunning as PHP’s definition:

Q: What does PHP stand for?
A: Hypertext Preprocessor
Q: ?!?

As anyone browsing the SitePoint Forums knows, the hottest debate right now is PHP vs. Microsofts .NET. Discussions range from reasoned interchange to wild argument; anything from “.NET — who cares?” to “.NET — the greatest thing since sliced bread”. What is clear from many posts is there’s a lot of uncertainty about what .NET actually is, and what impact it has on Web development.

This article is intended as “self-defence” for any PHP developers being harassed by over-exuberant .NET fanatics. It’s just one PHP’s coder’s point of view, but it should help clear up some of the most common misunderstanding, based on what’s been said at the SitePoint Forums in recent weeks.

The Top 10 Misconceptions

1. PHP is like an open source .NET.

You can’t compare PHP with .NET. .NET is an application framework (an environment for building and executing applications) not a programming language; it’s like comparing a truck to a road. PHP can, however, be compared to languages which run under .NET, such as ASP+ or C# (referred to as “Language.NET” here).

2. .NET is going to be the future of the Internet!

.NET is the future platform for all Microsoft technology — it’s not aimed solely at the Internet. In fact, it’s likely to make its first big appearance on corporate Intranets, with Office.NET and .NET versions of existing Windows NT server software.

3. .NET is the best technology for building a Website.

Both PHP and “Language.NET” are viable solutions for Web building. Whether it’s delivering a dynamic Web page generated from a database, sending an email, or interfacing with a payment gateway, both are perfectly capable. In assessing them as technologies for building your site, the only significant difference is the price. Try asking yourself “So how much does it cost to host this ASP.NET page I just wrote?”

4. PHP is not compiled, other than when it’s interpreted at runtime, whereas .NET enjoys all the benefits of being compiled.

Here’s news for the .NET crowd — you’ve finally caught up! Microsoft’s use of the word “compiled” in regard to .NET is not what a C++ coder would call “compiled”. Zend, the corporate face of the PHP project, have been doing this for a while now with PHP Accelerator (for a free equivalent, try php-accelerator.co.uk). Unfortunately for Zend, they chose to describe what Accelerator does as “advanced caching” (read more on what Zend means by “caching”). For more technical detail, try this thread at SitePoint Forums.

5. ASP.NET has accountable support. PHP has none.

Apart from the support services offered by Zend (effectively PHP’s creators), NuSphere, ThinkPHP and other corporations also provide support for PHP. You don’t need it, thanks to the ever-expanding PHP community being what it is, but if your boss wants someone credible to blame, there’s more than enough choice!

6. .NET supports multiple languages. PHP doesn’t.

Try Java for PHP or the upcoming Python extension. Obviously this isn’t quite the same as C# or J# but the point needs to be made — PHP is very flexible, and offers useful solutions for the Internet. Don’t expect to find yourself to be writing a successful Website in Cobol.NET — it’s likely each .NET language will eventually “tune in” to a particular type of application development.

7. Language.NET has superior object orientation.

For those who know PHP and its limitations, you may be amused by this .NET FAQ — sound familiar? In fact with PHP 4.3 in the wings, it may be that PHP can claim superior OO support, when you consider that FAQ.

What is significantly different is “Language.NET” will force you into an object oriented approach from the start, while PHP won’t. The Web typically does not require OO — for your average dynamic Website, a few “hacked” scripts are perfectly fine if they meets the site’s requirements. Deliver a live site now, rather than a beautifully abstract piece of code that’s ready one year after it’s needed. Paraphrasing SitePoint’s recent interview with Rasmus Lerdorf, PHP’s down-to-earth approach to problem solving is what has made it a success. And when you need OO, it’s there waiting for you.

8. .NET offers superior templating and web development features.

Microsoft has come up with some nice features with .NET that are certainly enticing, like the datagrid, and the templating mechanism. But you could easily reproduce these with a little PHP (try DaDaBIK, for example).

The basic notion of templating is to separate logic from content, so those with artistic skills can make a site that looks good, while developers apply the code “behind the scenes”. How Web designers feel about working with .NET’s templating system remains to be seen. With PHP, rather than being forced into a single approach, you have the choice of numerous Open Source projects (Smarty, Pat Template, etc.).

9. But corporations don’t like PHP.

Well apart from CBS, PHP is used on the corporate sites of Japan’s NTT DoCoMo, Switzerland’s Bluewin, and a whole load more you’ll find here. You frequently wont realise when you’re viewing a PHP-based site, because you’re not forced to use the default .php file extension (PHP often appears as .shtml or phtml).

Also, have a look at the results of this job search — not bad.

And as ezPublish demonstrates, corporations aren’t just using PHP, companies like Siemens are prepared to go into partnership to sell PHP applications to their customers.

10. PHP is doomed!

This is doubtful. In fact in June 2002, PHP overtook ASP to become the first Open Source project to start from behind, and become more popular than the bespoke Microsoft solution. With IIS going into decline against Apache as a result of the Apache 2.0 release and IIS’s poor security record, the gap will only widen.

The .NET framework has only one real edge over PHP, which won’t be making itself apparent for a long time yet: with .NET, you can write an application that will run natively on Windows clients and is delivered from a Website. Microsoft has a long way to go with security before it will be able to unleash that on the Internet — and even when that time comes, browsers and content delivery may be sufficiently advanced to make it irrelevant.

PHP is successful in its own right and in fact is in no way “threatened” by .NET. As to why, your guess is as good as mine, but top of the list might be:

  • its down-to-earth approach to problem solving (PHP has without doubt introduced to programming people who would never have thought themselves capable before),
  • its active and inspired community,
  • its comprehensive and efficient documentation (try this link for example: http://www.php.net/mysql), and
  • the freedom you have as a developer, both in terms of price and choice of code architecture, to do with PHP as you please.

Microsoft would do well to learn some lessons here. PHP.NET would be a start but at the moment Microsoft seems to be ignoring the Internet’s #1 scripting language.

Business as Usual

So there’s a little ammunition for harassed PHP developers. In summary, although .NET has some interesting features, there’s no pressing reason to switch to developing in it. Right now, in terms of web development, Language.NET really only amounts to ASP version 4.0 (with an option of writing in C++ style C#).

If you’re considering Language.NET, it’s worth asking yourself three questions;

  1. “Is there anything here I’m not already doing or capable of doing?”
  2. “Do I want to be forced into a sharp learning curve, a particular coding style and particular strategies for Web development?”
  3. “Do I feel like paying more for hosting?”

In answering those questions, you’ll have a pretty clear idea whether .NET applies to you. And rest assured you’re not “missing out” if you decide it’s not your kettle of fish.

Having said all that, .NET framework is an interesting technology. It incorporates some great concepts, such as multiple language support and has potential for seamlessly integrating the various Microsoft product families. It’s also good that .NET promotes XML standards like XML-RPC and SOAP, which have tremendous potential for the Internet and until recently, have been largely ignored, although XML-RPC has been around since 1999. But there’s a long way to go though before .NET is truly a pervasive technology — and the real showdown for the hearts and minds of developers is not “PHP vs. .NET” but J2EE vs. .NET, which is best left to another article.

All in all, we can now get back to business as usual, while thanking Microsoft for the diversion.

admin Programming, Uncategorized , , , , ,