CodeIgniter – Get controller or method name

Hey everyone!

Ever since I wrote a few posts on CodeIgniter and published Facebook PHP SDK for CodeIgniter library, and I have been asked this very same question many times. How to get name of current controller (Class) and method (Function)?

Its pretty simple one line code and can be easily found through Google. But there’s a lot of confusion and multiple codes depending on different versions. So I decided to share the code which I have been using on every website that I develop based on CI and works perfectly on the current version of CI v2.1.4.

That’s it! Just simple one line code. But there’s another thing I would like to share, Which is the best way to declare such variables so its available on all controllers and methods?

Obviously you are not going to execute same line on every controller or method (Seriously that’s a bad idea). This is really important as you will not be using just these 2 variables, you may also want site settings and menus to be available everywhere. So my idea to implement this is declare those variables in Controller.php file in CI_Installation/System/Core folder.

Instead of declaring $data variable to load view in every controller or every method. I prefer declaring it as array in Core CI_Controller class which is parent class of all controllers. So the $this->data variable is available and editable everywhere. Not just for variables, If you can also do some pre-processing such as loading data from cache and request validations by declaring functions and invoking them in constructor of CI_Controller class.

Here’s the sample code, this will give you an idea how I do it for my projects and if you have any better way, please feel free to share with me in comments!

Signing off! Puneet

Video Capturing In Java Using JMF

Hello Everyone,

I’m back with something new, while exploring Java and this time the API is : Java Media Framework (JMF).

Well this time I’m going to share how you can create Video Capturing Application Or you can say a Webcam in Java using JMF API.

Here’s a cap :

Cap Of Video Capturing Application Created In Java

No more words and without wasting more time. Lets get to the main point.

Here’s the code :  Click Here

Comments on all main lines of code are provided in file.

More to come soon!..

Screen Shot Using Java

Hello all,

I’m back ! Hope you all enjoyed Christmas. Today, I will tell you how to take screen shot and save it as .jpg, .png, .gif ( and so on ) file using java program.

Rectangle rectangle = new Rectangle(x, y, width, height);

// x = pixel from left

// y = pixel from top

// width = image width

// height = image height

Robot robot = new Robot();

BufferedImage image = robot.createScreenCapture(rectangle);

// Creates an image containing pixels read from the screen. This image does not include the mouse cursor and return BufferedImage object.

File file = new File(“screenshot.png”);

ImageIO.write(image, “png”, file);

// A class containing static convenience methods for locating ImageReaders and ImageWriters, and performing simple encoding and decoding.

//The code above will take screenshot according to rectangle object and it will save screenshot.png file in same folder where your java file is located.

//You can use Toolkit.getDefaultToolkit().getScreenSize() to take complete screenshot.

Dimension size = Toolkit.getDefaultToolkit().getScreenSize();

Rectangle rectangle = new Rectangle(0, 0, size.width, size.height);

Its done! 😀