My Craft CMS installation has public and craft in the root of the directory, with the domain pointing to the public folder. Where could I put custom PHP scripts that I can run in the browser?

I've tried it in the craft/templates folder (/craft/templates/m53ford). In this folder I can see the HTML or XML files by visiting http://feeds.rhinodigital.co.uk/m53ford/index.xml for example, but a PHP script doesn't run in the same place.

I've also tried in folders at the same root level (alongside the public and craft folders), and also within the public folder itself. Where in the directory tree could I place a PHP file that I can visit in the browser?

3 Answers 3


If you're simply looking to execute some custom scripts that are completely standalone (i.e. they don't interact with the Craft application in any way), those files should live somewhere below your public web root (i.e. the public folder, or wherever your index.php file resides). It isn't possible to have Craft route PHP files from the /templates directory.

However, the problem with reading/executing arbitrary PHP files from the public root, is that Craft basically "hijacks" all requests to anything below that folder, and routes those requests to index.php. In order to read or execute an arbitrary PHP file below the public root, you'll need to override Craft's URL routing, for example by using RewriteRule directives in your .htacesss file if you're using Apache (also see this related thread).

On the contrary, if your custom PHP scripts do need to interact with Craft in any way, you'll need to write a custom plugin.

Fortunately, Craft plugins are relatively simple to build (as long as you're familiar with PHP, of course). You can use the excellent Plugin Factory to quickly scaffold a bare-bones plugin with a thoroughly commented codebase or use the Business Logic plugin as a starting point. Downloading other available plugins and looking at their source code is probably not a bad idea, either.

In any case, before digging in you should definitely skim through the official docs on plugin development. By far, the most important thing to know about Craft plugins is how they are structured and what the various moving parts do. That knowledge will also give you a nice overview of what a plugin's capabilities are, and how you'd go about implementing a certain feature in your own plugin.

One thing that the official docs doesn't really mention, which might be relevant to your use case, is how you make use of third party scripts or libraries. There are lots of ways to skin that particular cat, but the headline is that using using Composer if you can is the best option, though a simple require() call in your plugin's main class' init method will of course also work.

Also, keep in mind that when you're using third party scripts inside a Craft plugin, you will need to deal with namespacing.


The upcoming Craft 3 release adds another option for running PHP business logic – Yii2 Modules.


So, you can't run php directly like that in CraftCMS, they disabled that for security reasons.

Instead, you will need to create a plugin to run your php code.

If you need the result returned in your template you can setup a twig variable then call it in your template, otherwise you can use other methods to run your code:

You can start here: https://craftcms.com/docs/plugins/introduction


You can add some business logic to your site using the Businesss Logic plugin. It's pretty straightforward.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.