{blog}


  • (PHP) Using route groups in Laravel to protect routes

    To protect routes, in Laravel, from unauthorized visits you can use route groups.
    All routes in group will be affected by the auth before filter.

    [php]
    Route::group(array(‘before’ => ‘auth’), function() {

    Route::get(‘controlpanel’, ‘CustomerPanel@showPanel’);
    Route::get(‘secret’, ‘Secret@showSecret’);
    Route::post(‘setup’, ‘SetupController@doSetup’);

    });

    [/php]


  • (jQuery) Only allow number keyboard input

    $("input").on("keypress", function (e) {
        if (!jQuery.isNumeric(String.fromCharCode(e.which)))
            return false;
    });
    JavaScript

  • (PHP) simple picture/file upload script

    Here is a simple tutorial script that uploads a file to a folder on the server. In this example it is assumed that the file is an image and it will be outputted on screen in an img-tag. But the file uploading mechanics works for any file type.

    [php]
    <?php
    if(isset($_POST[‘submit’]))
    {
    $temp_name = $_FILES["file"]["tmp_name"]; // get the temporary filename/path on the server
    $name = $_FILES["file"]["name"]; // get the filename of the actual file

    // print the array (for reference)
    print_r($_FILES);

    // Create uploads folder if it doesn’t exist.
    if (!file_exists("uploads")) {
    mkdir("uploads", 0755);
    chmod("uploads", 0755); // Set read and write permissions of folder, needed on some servers
    }

    // Move file from temp to uploads folder
    move_uploaded_file($temp_name, "uploads/$name");
    chmod("uploads/$name", 0644); // Set read and write permissions if file

    }
    ?>

    <form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="file" id="file" />
    <input type="submit" name="submit" value="submit"/>
    </form>

    [/php]


  • How to make OSX use MAMP php instead of system version

    Getting OSX to install Laravel properly is a minor nightmare.
    Here are some good instructions on how to get it to use MAMP php instead of the broken default one that comes with OSX.

    https://gist.github.com/irazasyed/5987693


  • (Android) Delay method call

    This is how you could delay a method call for a given amount of time. I’ve used this to have an alert dialog pop up a few second after starting an activity.

    This will fire myMethod() after 3000 miliseconds:
    [java]
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
    @Override
    public void run() {
    myMethod();
    }}, 3000);
    [/java]


  • (Android) Check if Google Play Store is installed on device

    Here’s a quick way to check if Google Play Store is installed. I use this to keep activities that use in-app billing from crashing on devices that doesn’t have Google Play Store. Works fine in the emulator as well. If the method returns false, I’ll just give the user an alert telling them they need Google Play for in-app billing. If it returns true the in-app billing system gets set up.

    Here, implemented in a static method. If you call this from an activity you go:
    [java]
    boolean myBoolean = googlePlayStoreInstalled(this);
    [/java]
    and if you’re in a fragment or so just go
    [java]
    boolean myBoolean = googlePlayStoreInstalled(getActivity());
    [/java]

    I keep this in a utility class.
    [java]
    public static boolean googlePlayStoreInstalled(Activity activity) {

    PackageManager packageManager = activity.getApplication().getPackageManager();
    List<PackageInfo> packages = packageManager.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);

    for (PackageInfo packageInfo : packages)
    if (packageInfo.packageName.equals(GooglePlayStorePackageNameOld) || packageInfo.packageName.equals(GooglePlayStorePackageNameNew))
    return true;

    return false;
    }
    [/java]


  • (Android) Simple confirm dialog

    [java]
    private void confirmDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);

    builder
    .setMessage("Are you sure?")
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int id) {
    // Yes-code
    }
    })
    .setNegativeButton("No", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog,int id) {
    dialog.cancel();
    }
    })
    .show();
    }
    [/java]


  • (Android) Fast Android emulator

    This Android emulator is fantastic www.genymotion.com


  • (Android) Tabbed activity with fragments tutorial

    Here is a really nice and clean tutorial for making an activity with tabs.
    http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/


  • CSS font stack

    Great site for pulling out css for webfonts: http://cssfontstack.com