zend framework 2 blog module

Dear WordPress,

After many years of fiddling with you, it seems I have finally reached my breaking point. It started off as exciting, hot, heavy and fun, like most relations do, but it has become an annoyance to deal with you. The resources you ask of me, the many confusing options you present me with… It’s just too much. I can’t deal with it anymore, and I want to be able to be myself again! I’ve had it with adapting to your needs just so I won’t hurt your feelings.

So it seems that the time has come for me to build my own blog based on Zend Framework 2. I’ve already started with the module and it has been a joy working on it so far. For now, it includes the following features:

  • An overview of posts
  • An overview of categories
  • An overview of posts by category
  • Pagination
  • Create new post
  • Delete post
  • Edit post
  • View post
  • Create new category
  • Delete category
  • Edit category
  • Customisable messages (for deletion, creation, updates etc)
  • Theme support (really just by supplying view files. Extended support will follow)

The module can be found here: https://github.com/SpoonX/SxBlog

There’s more to come, but for now this is more than enough for me to leave you. It’s just younger, more organised and it listens to me. And in the end, it was ‘The Loop‘ that drove me away from you…

It’s not me, it’s you. But you will forever hold a place in my memory. I wish you the best of luck. in the years to come.

No longer yours,

Roberto Wesley Overdijk.


PHP sandbox online editing

I’m guessing you got to this page because you like PHP, and you like testing without creating entire directory structures or using php -r ”;.

Well, me too. That’s why I went out trying to find a tool that could help me with exactly that. One problem! There were none. And the ones that I did find also did not work the way I wanted them to. They were either online, too bloated, or required ftp access.

So, I built one. Yes, yes I did. You can find the code here: https://github.com/RWOverdijk/SxPhandbox

Installation should be pretty straight forward, as explained in the readme.

Runtime errors in the editor

Have fun!


Redirecting from a controller in ZF2

Hey all. Today’s post is a response to a question asked today on IRC.

The question:

In zf1 within a controller I could do something like $this->getResponse()->setRedirect(‘/foo/bar’, 201) ~ How do I do that using zf2?

The answer:

Controllers have plugins, not much has changed there. Except for the fact that it’s now even easier to get a redirect going. Check this out:

$this->redirect()->toRoute('routeName');

It also has a toUrl method, which behaves the same way:

$this->redirect()->toUrl('http://google.nl/');

Counter question:

Can you send a statusCode with that though?

Counter answer:

Yes, you can. As seen here, the redirect returns a Response. A response has a setStatus code, thus allowing us to do this:

$this->redirect()->toRoute('routeName')->setStatusCode(201);

Happy coding :)

 


Asset manager module for zend framework 2

Not very long ago I published the first stable version of the AssetManager module for Zend Framework 2. The purpose of the module is to allow you to package assets with your module, in stead of having to mess around with symbolic links, aliasmatch or mod_rewrite. On top of that, this module also gives you some other functionality such as caching, grouping assets and applying filters to your assets. You can add custom filters, custom cachers and custom asset resolvers.

https://github.com/RWOverdijk/AssetManager


Validate existence of youtube video using javascript and jquery

There’s no need to include this snippet. The title says it all.


unit testing unlink

Writing unit tests is very important. However, sometimes it requires you to be creative as it doesn’t always allow you to do what you want to. One of these things is testing a failed unlink.

I was testing a file system caching adapter using phpunit, and this became quite the challenge. Testing the has, set, and get were easy peasy, but removing the cache was rather difficult. I wanted to make sure that I didn’t have permissions to remove the files in question. I worked my way around it by simply trying to remove /dev/null. This is not allowed and results into the error I wanted.

I hope this is useful for you.

Cheers.


SPI Firewall Protection on router and macosx

Le introduction

I recently moved to a new apartment with a nice garden and a bigger bedroom. Everything seemed to be perfect, the couch fits nicely and my cat loves the new garden. It all seemed too good to be true, and it was. My internet was not working, for some odd reason. After a long and hard battle I managed to fix it, and decided to blog about it in case anyone else ever faces this challenge. I have supplied the solution on the bottom of the post for the impatient people.

The quest begins

The first thing that came to mind was doing a simple “ping google.com” to see if there was any network activity at all. This failed and so I started going through my previous experiences to see if I could determine what the problem was. Thus started the adventure of making a checklist.

  • Can other device connect to the network? yes
  • Do they have internet access? yes
  • Conclusion: Machine specific. (later I learned that I was wrong.)
A rather small checklist. I was stumped, and tried to connect to one of my development servers through ssh to see if I could ping myself from there. Oddly enough, after connecting to my development server, it took me surprisingly long to realize that I had just connected to a remote server over the internet, and thus my internet _was_ working. Shocked I tried launching skype, Colloquy (IRC) and refreshing my browser. A couple of things surprised me, but also provided me with a great deal of information:
  • SSH worked
  • Skype worked
  • IRC worked
  • Browser didn’t work
  • Ping didn’t work
Aha! Something was up with port 80.

From realization to desperation

After realizing this I found myself in some unknown territorium. I’ve never seen anything like this before and so I was rather surprised. This followed with me doing some random things in desperate attempts to fix it. Restarting the router, rebooting my iMac, flushing dns cache, flushing browser cache and so on. I was confused, and never stopped to think that the problem might be something in my router. Why? Because (don’t laugh) I had reset my router 7 times. So I decided to check every, single, setting on my iMac. This was a long, time-consuming but most of all fruitless attempt. I decided to see if the guys in #macosx on freenode had anything helpful to say. The first thing they asked was if I had a proxy, which I already checked. They then continued pointing out various possible settings that could cause this odd behavior, all of which I had already checked as well. Then they pointed out that it must be a setting in my router.

Like I said earlier, I already had reset the router 7 times, so I doubted it’d be a setting in there. I checked anyway. After disabling “SPI Firewall Protection” and performing a “dscacheutil -flushcache” everything worked again. Every other device got along with this perfectly, but my iMac decided not to. I’m not sure why this works, but it does. As soon as I figure out why it works I will write a follow-up.

Solution

Disable “SPI Firewall Protection” in your router and perform a “dscacheutil -flushcache” on the command line. Also, it wouldn’t hurt to check your DNS settings (to see if you have the correct settings) and test other browsers first.

Cheers


RequireJs followup : The creation of a module

Introduction.

Last time I wrote about a possible require JS module for zend framework 2. After receiving a fair amount of interest, feedback and suggestions in general I decided to give it a go.

I proceeded to spend a decent amount of time reading all of requirejs’s documentation, mapping out how I’d like to fit that structure into the zend framework modular structure. It’s pretty much the same as described in the previous blog post, with only one big change… I’ve actually written the code. I know, shocker right.

So now what?

Well, I know this news might not be as interesting as I hoped it would be, but it is something. By publishing this blog post I’m hoping to receive more feedback, and perhaps some nice PRs. I’ve written some usage examples in the wiki in case you’re interested.

Applications

RequireJS (from here on RJS) did an awesome job supplying a modular structure. However, when working with ZF2 you’re already in a modular structure. This means we’re bound together with other modules if we want to share RJS modules. Every ZF2 module’s RJS module should have the possibility to get executed. Imagine having a RJS module that doesn’t get executed because the main application doesn’t know about it. That would take away a lot of flexibility.

I kept in mind that every module should share the same config map when it comes to loading RJS modules. This means that one way or another, we’ll still need a main application to provide all modules with this information.

SxRequireJs sets up a main application, which essentially sets all “module bootstraps” as a dependency, waits for them to load, and then executes them. This will be your module’s entry point, and should have its own dependencies.

Finally

Yes yes, I’ll stop talking. But I’d like to ask all of you to take a look, leave feedback and / or submit improvements.

Cheers.


RequireJS implementation for Zend framework 2

I have written a followup on this post, where I explain that the module is already available.

This is my initial idea for a requireJS module that could be used in zend framework 2. There’s not a lot of details yet, but the logic is there. Implementing this will be the easiest part, as I do believe that the architecture for this should be just right because you otherwise lose the flexibility and structure that both zend framework 2 and requireJS offer. I guess I’ll just start of by explaining my personal zend framework 2 architecture a little bit.

I do not store all of my assets in one directory. 

My approach is slightly different, in the sense that I supply assets on a per module basis. Every module has a public directory, and I use AliasMatch and RewriteEngine to map requests to their assets:

 

So basically I rewrite requests from this “/mymodule/js/main.js” to this “/path/to/application/module/Mymodule/”. This way my modules actually supply their own assets.

 

The reason I started using requireJS should be obvious to everyone that knows what it is.

In case you still want to know why,  it’s because I like structuring and setting up dependencies in a proper manner. This allows me to have a “main” module that will take care of global modules / dependencies such as jquery, allowing me to quickly scan for modules that don’t support jquery1.7.3 yet (example).

 

The problem with requireJS is the structure it expects.

With the way zf2 modules are set up the optimizer will not work. Also, being able to have one entry point (for the “main” requireJS) will be rather difficult with a bunch of modules having their own “main”. This could be solved by mapping those to the main application but then you’d not be working modular.

 

My solution is an implementation that will figure all of this out for you.

I was thinking of building a zf2 module with a viewhelper that allows you to register a path for your zf2 module as well as the main requireJS module. zf2 modules register requirejs modules, and at the end of the run the viewHelper will simply set up the requirejs config (paths, so that /zf2module/* links to the actually resource) and dependencies to make sure that all requirejs modules get loaded.

When enabled, this run could also automatically create a build file for requirejs. This, however, will work event based (zf2) and will ask every module for their requirejs modules (because a build takes all js). This allows you to leave out very specific requirejs modules from the build. Along with this approach, you could also have a build.json / build.js for every zf2 module which would be scooped out by the optimizer (a zf2 layer that will create the build file based on these files).

 

The reason I think this approach is awesome is because of its flexibility.

Simply because using this approach allows you to not have to think about your structure too much (the main goal behind requireJS and zf2 modules) and focus on doing what you know, thus your logic. It’s flexible enough for you to write your own implementations and use this the way you think fits best, while still keeping all advantages of requireJS.

 

Some examples, maybe?

I do not have a working module yet, as I’m still collecting feedback and ideas. I do however have some really simple uses for the view helper.

I do realize this illustrates the usage for your main module view, however it might give you a better idea of the structure I’m thinking about.

 

Any questions, tips or feedback?

Do tell me. This is just the theory and an initial proposal. So all feedback both positive and negative are well appreciated.

 


jQuery script timestamp

jQuery appends a timestamp to your script path when using getScript. To disable this behaviour just disable it in the jquery ajaxSetup like this:

$.ajaxSetup({
cache: false
});


©SpoonX 2013