Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8815957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:30:14+00:00 2026-06-14T04:30:14+00:00

I have a helper which change different strings to dates. By this +5 becomes

  • 0

I have a helper which change different strings to dates.
By this +5 becomes 2012-11-17 (5 days from today), 5 becomes 2012-11-05 (this year, this month, given day).

I would like to use the same thing in my controller.

App::uses('EasyDateHelper', 'View/Helper');
App::uses('View', 'View');
$this->View = new View($this->Controller);
$easyDate = new EasyDateHelper($this->View);

This code works well, but i would like to know if there is a better / simpler way to do.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:30:15+00:00Added an answer on June 14, 2026 at 4:30 am

    Yeah, there is.
    Don’t make it a helper!

    Helpers are by design only for the view level.
    If you need it in a controller, your design is wrong.

    So make it a lib class etc and use it in your controllers this way.
    and either use the same lib in your helpers or make a wrapper helper (similar to TextHelper for String, NumberHelper for CakeNumber, TimeHelper for CakeTime etc) to easily access the methods in your view scope.

    For more complex solutions take a look at Sessions:

    CakeSession is the lib class.
    SessionComponent wraps it for the controller layer
    SessionHelper wraps it for the view layer
    

    For a Lib “DateLib” (you can name it whatever you like as long as the class name is unique throughout app and core):
    Either put it in /Lib/ or /Lib/PackageName whereas PackageName can be defined by you (e.g. “Utility”). It is better to group by PackageName instead of putting it in Lib directly.

    /Lib/Utility/DateLib.php
    

    And in your code:

    App::uses('DateLib', 'Utility');
    
    $date = DateLib::format(); // for static methods
    
    $DateLib = new DateLib();
    $date = $DateLib->format(); // creating an extra object here
    

    if you can work with static methods you should. this is also the way cake does it for the CakeTime, CakeNumber and String classes (see the code for details).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a helper assembly which includes a function to identify object types: namespace
I have two helper methods inside a custom View which are called by their
I have a helper function, which basically calls CompareTo on two objects, but does
Hi I have a helper method, which returns a string, where i need to
ok so i have this helper def playlist_count(user, site_id) user.companies.local(site_id).map(&:playlists).flatten.count end which will return
I have an array of Ember.Object s which is displayed by Handlebars {{#each}} helper
I have a JavaScript function (which users on this forum graciously helped me construct)
I have a helper class that I want to display a UIAlertView then will
I have a helper class that should only ever run in a background thread.
I have a helper class that is passed an array of values that is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.