Tuesday, March 1, 2016

Set Up Common Controller Code in OpenCart

If you've come across module development in OpenCart, you may have faced the situation in which you've declared a couple of common elements in each and every controller. Don't you think that it would be nice if you could declare the common code somewhere and it's picked up as needed! So in this article, we're going to discuss how to set up common controller code and use that across the modules.

For those who are familiar with module development in OpenCart, it's a routine to set up the common elements like header, footer and sidebar columns in the index method of the controller class. Although there are different ways to centralize the common code, we'll look at an OpenCart way to accomplish this!

I assume that you're using the latest version of OpenCart and are familiar with the module development process, as we'll concentrate more on the concept rather than discussing the basic code.

Set Up the Common Controller Code

Go ahead and create the catalog/controller/preactiondemo directory. Create a file common.php under that directory with the following contents.

Fairly easy and straightforward to understand! It just initializes a couple of variables in the $data array, except the last line of the setup method. We'll get back to that later, as it will reveal the secret of the $args array.

Understand the Dispatch Process

Before we create any further code, I'll give you a quick explanation of how dispatching works in OpenCart.

Whenever the user accesses any URL in OpenCart, the corresponding action object is instantiated based on the route query string variable. Here's the snippet from index.php.

And following that, the dispatch method is called.

It’ll call the dispatch method defined in the file located at system/engine/front.php. In that method, you’ll find a snippet which executes the while loop until it gets the $action value set to false.

As you can see, it’ll call the execute method defined in the same file until $action evaluates to false. This means that if the method of the controller returns an action object, OpenCart will execute that action before proceeding further. We could take advantage of this and call the other action from within the action itself. Let’s see how to accomplish that!

Call the Common Controller and View Setup

Now, let's create a preaction_demo.php file under the preactiondemo directory with the following contents.

If $flag is true, we’ll return the instance of the action class, and as we’ve just seen, if the dispatch process receives an action object, it’ll continue with that action. So in this case, it will call the setup method of the common controller. Recall the common.php file which we created in the earlier section.

The important thing to note is that we’re passing array('controller' => $this, 'method'=>'index') as an argument, which will be eventually passed to the first argument of the setup method in the common controller. It’ll help us to get back to the index method of the preaction_demo controller after initialization of variables.

Further, in the setup method we’ve defined a few common variables like header, footer, etc. And finally, we’re transferring control back to the original controller from where the setup method was called using the following statement.

Of course, we need to pass the variables initialized in the setup method via $data to the original controller so that it can be utilized there, which is the main purpose of this article. It’s passed as the first argument of the above method call. The second argument is very important as it’ll be initialized to the $flag variable. We’re deliberately doing this to avoid infinite looping.

Finally, let’s go ahead and set up our view file at catalog/view/theme/default/template/preactiondemo/preaction_demo.tpl with the following code.

Now that we've finished with the setup, go ahead and open the URL http://youropencartstoreurl/index.php?route=preactiondemo/preaction_demo in your browser. You should see the message "I've been set by the 'setup' method from the 'common' controller file." in that page!

So, in this way you could set up the common code in one place, and call that action from the other controllers. Certainly, it’ll avoid code duplication, and it’s easier to change the code which is common across the modules.

Conclusion

Today, you’ve learned a cool trick in OpenCart: how to call an action from within the action! Of course, there are other ways to achieve that, but it’s something I’ve got in my mind lately and thought I should share it with you and get your feedback!


by Sajal Soni via Envato Tuts+ Code

Quiz: JavaScript ES6, Do You Know the Right Tool for the Job?

JavaScript ES6 (also known as ECMAScript 2015) has brought a bounty of new syntax intended to improve developer experience and ergonomics.Take this quiz to test your knowledge of JavaScript ES6.

JavaScript ES6 Fundamentals

JavaScript ES6 (more properly known as ECMAScript 2015 or ES2015) represents the biggest change to JavaScript in the last 20 years. JavaScript is the language of the web, and ES6 is a major improvement for web developers, adding much-needed features such as classes, promises, arrow functions, generators, string templates and many others. Browser support is growing, and transpilation tools such as Babel allow you to get started coding in ES6 today.

In this course, JavaScript ES6 Fundamentals, Envato Tuts+ instructor Dan Wellman will help you sharpen and update your JavaScript skills by learning some of the most important additions that ES6 brings to the language. Cutting-edge web development practices are increasingly relying on ES6, so knowledge of the new syntax is becoming more and more necessary. Not only that, but programming in ES6 is just more fun. So let's get started!


by Adam Brown via Envato Tuts+ Code

How to Customize Slack for Social Media Teams

cs-customize-slack-560

Is more than one person involved with your social media marketing? Are you looking for a way to improve collaboration on marketing projects? Whether your team is within an office building or scattered around the world, Slack’s third-party app integrations make it easy to create a customized collaboration center. In this article you’ll discover how [...]

This post How to Customize Slack for Social Media Teams first appeared on .
- Your Guide to the Social Media Jungle


by Chloe Swain via

4 Tips to Improve Your YouTube Marketing

es-4-youtube-tips-560

Are you marketing on YouTube? Could you use some new ideas to improve your YouTube results? Perhaps you should reconsider your YouTube approach. In this article you’ll discover four tips to make your YouTube marketing more effective. #1: Pique Curiosity With Unconventional Video People aren’t going to click on your video to see a boring infomercial. [...]

This post 4 Tips to Improve Your YouTube Marketing first appeared on .
- Your Guide to the Social Media Jungle


by Eric Siu via

H-Code

H-Code | Responsive & Multipurpose WordPress Theme

'H-Code' is a robust One Page WordPress theme that covers multiple industries. To give you an indication of spread there are 11 different One Page layouts that includes restaurant, fashion, architecture, spa, digital agency, corporate, travel agency, personal wedding and a standard landing page. Themezaa have also mixed up the features with the layouts giving your 26 One Page demos to browse and really experience the theme. Featured here is my favorite, the restaurant option. Just love how the ingredients slide in - achieve by the Slider Revolution plugin - valued at $19 and bundled within the price. It's a monster theme with huge value for your money. Good to know, there is also an HTML version for $17 if that's all you're after.

by Rob Hope via One Page Love

Juice VCR

opl-small

Minimal One Pager for 'Juice VCR' a online web channel showcasing the latest visuals from DIY + independent artists.

by Rob Hope via One Page Love

Dac Davy Nguyen - DDNG Portfolio

Dac Davy Nguyen — DDNG portfolio / Interactive designer
by via Awwwards - Sites of the day