Tag: coding

  • Laravel 5: Create controller under new folder and routing

    So here I am on a Sunday, trying to make progress with a personal Laravel 5 site I need to work on. As I bear with the noise outside in the street (some crazy neighbors set up a swimming pool IN THE STREET! :oops:), I’m trying to figure out how too create a new Laravel 5 controller and place it inside a sub-folder.

    Thanks to a StackOverflow answer as my guide, it’s now working. Here, I’ll share how I did it. Just replace “New” with whatever name you want.

    First, create NewController.php on this folder: app\Http\Controllers\New\

    Then the first part of the code should be like:

    My call for the blade file in index() looks like this:

    In app\Http\routes.php, add this:

    $router->get(‘/new’, ‘New\PostsController@index’);

    Last, run composer dump-autoload or php artisan dump

    Guide: Laravel Controller Subfolder routing

  • Android Development: Permission denied when running ./gradlew in Mac

    I’m using a Mac at the moment to work on my Android app projects.

    For some reason I need to run ./gradlew in the terminal, but I was getting this error:

    -bash: ./gradlew: Permission denied

    To fix this, what I did was run the following commands:

    After those commands, my Mac downloaded numerous files and it took an hour to finish because of our poor Internet connection in the workplace. 🙁

    After an hour and a half, I was able to finally use the command ./gradlew on the Mac Mini. Phew!

  • Android Studio and the 64K methods limit (DEX error)

    Hi! I’ll share a few tips with you on how to find out the number of methods that your Android app has. I’m gonna show you a few tools such as APK file methods counter, and an Android plugin that will show you the number of methods each imported class or dependency has. These will help you figure out if you’re nearing the 64K methods limit, and determine which classes/dependencies must be causing it.

    First off, try this online drag-and-drop tool that will show you the total method count as well as breakdown based on the APK file you’ll use: APK Method Count

    Now, let’s install the plugin. After installation, you should be able to see method counts on your app/build.gradle file.

    Install plugin to check for unused dependencies: Jonnyzzz Dependencies. I used version 1.0.10. To do this, in [Windows] go to File -> Plugins, or in [Mac] go to Android Studio -> Preferences. Press “Browse Repositories” then find and install Jonnyzzz Dependencies. Restart Android Studio.

    To use, from the menu, select Tools -> Unused Module Dependencies -> Analyse All Modules

    You’ll be prompted for an option to remove all unused dependencies. I could not do that because I had a team mate who might use some of those in the list, so I manually deleted libraries that I’m sure we no longer need.

    Sync Gradle, Clean, or even Rebuild… and run your app again.

    I must admit that this DEX file error is a nightmare to me. xD I used to only code my Android app in a Windows 7 computer, which was very slow. Then some office friends decided to lend me a higher-specs Mac Mini that nobody’s using. It’s helping a lot, but the app I’m working on in Windows is getting 64K methods limit in Mac. I had the exact same copy of my Android Studio project in both Windows and Mac computers, but only the one in Mac was getting 64K methods limit error. 😆

    Now I’m thinking of the small differences in my Windows’ and Mac’s config for Android Studio…

    Could it be because ‘com.google.gms:google-services:2.0.0-alpha9’ and Android Studio v.2.1 in Mac, while my Windows 7 had ‘com.google.gms:google-services:2.0.0-alpha6’ and Android Studio v.1.4?

    From what I understand in the Methods Count pages for Google Services 2.0.0 Alpha 6 and Alpha 9, Alpha 6 has more methods than Alpha 9.

    My Android Studio v.1.4 in Windows using Alpha 6 (more methods) but the app runs without problem, while Android Studio 2.1 in Mac using Alpha 9 (less methods) triggers the 64K methods limit. 😆

    I don’t want to keep thinking about this right now because I have pending tasks to do. 😆 But it’s nice to know roughly what we can do next time I get this dreaded 64K methods limit error.

  • CSS / JavaScript: Twitter Bootstrap center modal vertically on mobile web view

    I’ve been working with modal boxes using Twitter Bootstrap. And the page I’m working on needs to be viewed in mobile devices. The modal looks nice and centered when viewed on desktop, but when run in mobile web browsers, it’s aligned to the top.

    Someone on StackOverflow suggested the use of a helper div plus some custom CSS to align Bootstrap modals vertically.

    Code for modal:

    CSS:

    Source: Vertically centering Bootstrap modal window

  • JavaScript: Get current function name

    getCallingFunctionName() is the important function here.

    Source: Can I get the name of the currently running function in JavaScript?

  • Android: Switch into another activity

    To switch into another activity, the method we use is startActivity().

    This is what starts another Android activity:

    Replace NameOfActivity with the name of any activity you’d like to switch into.

  • Android: Basic WebView Tutorial

    A WebView lets you pull a web page and display it in your Android app. It doesn’t take long to implement an Android WebView. Read on. I’ll show you how.

    AndroidManifest.xml

    First, open your manifest file to add this permission for INTERNET:

    The web page won’t be loaded into your app without this permission.

    This is how my manifest file looks like:

    Path: ...\MyAndroidStudioProjects\WebView\app\src\main\AndroidManifest.xml

    activity_main.xml

    Then we’re adding a WebView element to the XML file. My XML file is named activity_main.xml

    Path: ...\MyAndroidStudioProjects\WebView\app\src\main\res\layout\activity_main.xml

    MainActivity.java

    I have MainActivity.java here, which loads activity_main.xml and then loads a web page (google.com) into the WebView.

    Path: ...\MyAndroidStudioProjects\WebView\app\src\main\java\net\catzie\webview\MainActivity.java

    The following line of code attaches the WebView whose id is “webview” to the variable “webView”:

    And this allows JavaScript in the WebView:

    This one is what actually loads a given URl into the WebView:

  • PHP dynamic variable names tutorial

    I think it’s very convenient for us developers, being able to name our variables dynamically. I’ll show you how to do it in PHP. 🙂

    For example, we have variable $foo with “bar” as value like this:

    If we want to create a variable named based on the “value” of variable $foo, this is what we write:

    The echo should display: My variable name is bar.

    Notice that the second variable that we declared was written as $$foo with 2 dollar symbols before the variable name. This makes it a variable variable — a variable whose name is a variable. What happens is that it first gets the value of $foo, which is “bar” and then uses that value as the variable name. And so, in the example above, defining the variable as $$foo = "My variable name is bar." is equal to defining it as $bar = "My variable name is bar.".

    Making the PHP variable name more dynamic

    Now, what if you want to make your variable name more dynamic? I’ll give you another example.

    Here we are generating a random 1-digit number, we’ll use it later:

    The way that we’ll define the dynamic variables will be a bit different from the example earlier.

    And here we have an array of strings:

    What if we want to create variables whose names are based on the value of strings in the array, like $uno, $dos, and $tres?

    We do it like in the code below. Notice that I defined my dynamically named variable like ${$v} inside the foreach:

    Now let’s use the $numbers I showed you earlier. Pay attention to what I put inside the foreach.

    The foreach let us define variables whose names are $uno1, $dos2 and $tres3, which are concatenated values from arrays $numbers and $words.

    This is where the magic happens: ${$words[$x] . $numbers[$x]}

    We combined elements from $words and $numbers arrays, then turned them into variable names by enclosing them in ${}

    And that’s it! Use variable variables at your convenience. 🙂

  • PHP: get full URL of current page with GET params or without

    The following string variable’s value will get the full URL of the current page, including GET arguments/params, if present.

    To get the full URL of current page but WITHOUT the GET args/params, do this instead:

    The strtok function will return only the part of the URL string up to the character before question mark “?” which is used by GET args/params.

    strtok was suggested in this StackOverflow answer: http://stackoverflow.com/a/4643312

  • PHP: avoid backslash on json_encode

    Hey! 🙂 If you are encoding your Arrays to JSON in PHP using json_encode() and the function adds backslash before your slashes, I’m gonna show you a quick fix.

    This is my original code:

    And the result is:
    {"success":true,"message":"Please enter email\/phone number."}

    See the added backslash before the slash? Ugly, right? To avoid that, simply add the JSON_UNESCAPED_SLASHES argument to your json_encode() function usage like this:

    Try it!