CodeIgniter Snippets for Visual Studio

I thought I would share something I have been using for quite a while now and find incredibly useful. They are two snippets for Visual Studio that create the standard file start and file end for php files in the codeigniter framework. If you are not sure what I mean then check out the files that make up the framework – you will notice that they all tend to start with:

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

and end with:

/* End of file MyFile.php */
/* Location: ./path/to/file/MyFile.php */

The line at the start of the file is a security measure to stop files being executed outside the framework, whilst the end is just informative but useful anyhow.

I like to do the same thing with all the files I create for my applications but found the typing tedious and because I use the VS.php in Visual Studio, I decided to write a couple of snippets to do it for me.

If you are not familiar with snippets in Visual Studio or how to get the following xml to work as a snippet, you can read all about then on msdn. Anyhow, without further ado, here they are:

filestart.snippet:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>filestart</Title>
      <Author>Dan Sargeant</Author>
      <Description>CodeIgniter File start to stop direct script access</Description>
      <HelpUrl></HelpUrl>
      <SnippetTypes>
          <SnippetType>Expansion</SnippetType>
	      <SnippetType>SurroundsWith</SnippetType>
	  </SnippetTypes>
      <Shortcut>filestart</Shortcut>
    </Header>
    <Snippet>
      <Code Language="PHP" Kind="" Delimiter="$"><![CDATA[<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

fileend.snippet:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>fileend</Title>
      <Author>Dan Sargeant</Author>
      <Description>CodeIgniter file ending</Description>
      <Shortcut>fileend</Shortcut>
      <SnippetTypes>
          <SnippetType>Expansion</SnippetType>
	      <SnippetType>SurroundsWith</SnippetType>
	  </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>filename</ID>
          <ToolTip>Filename</ToolTip>
          <Default>myfile.php</Default>
        </Literal>
        <Literal>
          <ID>directory</ID>
          <ToolTip>Directory</ToolTip>
          <Default>models</Default>
		  </Literal>
      </Declarations>
      <Code Language="PHP"><![CDATA[
/* End of file $filename$*/
/* Location: ./application/$directory$/$filename$*/]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>
 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.