Posts

Showing posts from April, 2017

Get the local IP address using Java.

Image
There are many ways to get the local IP address using Java. And we are going to examine various methods. On the face of it, InetAddress.getLocalHost() should give you the IP address of this host. But it doesn't. One of the major problems are that a host could have lots of network interfaces, and an interface could be bound to more than one IP address. And to top that, not all IP addresses will be reachable outside of your machine or your LAN. For example, they could be IP addresses for virtual network devices, private network IP addresses, and so on. Following method use opendns.com to get the local IP address. package learnjava.System; /** * Created by Isuru on 11/04/2017. */ import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; // class with main() method public class IPAddress { public static void main(String[] args) { String ipAdressDns = "" ; try { String command = "nslookup myi

Redirects in Laravel 5.4

There are two common ways to generate a redirect; we’ll use the Façades here, but you may prefer the global helper. Both create an instance of Illuminate\Http\RedirectResponse , performing some convenience methods on it, and then returning it; you could do this manually, but you’ll have to do a little more work yourself. Redirect with facade Route::get('redirect', function () {        return Redirect::to('auth/login'); }); Redirect with helper Route::get('redirect', function () {        return redirect()->to('auth/login'); }); Redirect with helper shortcut Route::get('redirect', function () {        return redirect('auth/login'); }); When a user visits a page they’re currently not authenticated for—for example, visiting  a dashboard when their login session has expired—Laravel captures their  intended URI and redirects back to it after a successful authentication. This is performed  using guest() and intended() .  R

Form method spoofing in Laravel 5.4

To inform Laravel that the form you’re currently submitting should be treated as something other than POST, add a hidden variable named _method with the value of either PUT, PATCH, or DELETE, and Laravel will match and route that form submission as if it were actually a request with that verb. <form action="/tasks/5" method="POST">       <input type="hidden" name="_method" value="DELETE"> </form> It passes Laravel the method of “DELETE,” will  match routes defined with Route::delete but not those with Route::post.

Passing route parameters to the route() helper in Laravel 5.4

There are a few different ways to pass these parameters. Let’s imagine a route defined as users/{userId}/comments/{commentId}. If the user ID is 1 and the comment ID is 2, let’s look at a few options we have available to us. Option 1 route('users.comments.show', [1, 2]) // http://9url.ml/users/1/comments/2 Option 2 route('users.comments.show', ['userId' => 1, 'commentId' => 2]) // http:// 9url.ml/ users/1/comments/2 Option 3 route('users.comments.show', ['commentId' => 2, 'userId' => 1]) // http:// 9url.ml /users/1/comments/2 Option 4 route('users.comments.show', ['userId' => 1, 'commentId' => 2, 'opt' => 'a']) // http:// 9url.ml /users/1/comments/2?opt=a Non-keyed array values are assigned in order; keyed array values are  matched with the route parameters matching their key and anything left over is  added as a query parameter.

Regular Expression Route Constraints And Route Names in Laravel 5.4

You can use regular expressions to define that a route should only match if a parameter meets particular requirements. Route::get('users/{id}', function ($id) { // })->where('id', '[0-9]+'); Route::get('users/{username}', function ($username) { // })->where('username', '[A-Za-z]+'); Route::get('posts/{id}/{slug}', function ($id, $slug) { // })->where(['id' => '[0-9]+', 'slug' => '[A-Za-z]+']); If you visit a URI that matches a path, but the regex doesn’t match the parameter, it won’t be matched. So users/abc in the example above would skip the first Closure, but it would be matched by the second Closure, so it would get routed there. On the other hand, posts/abc/123 wouldn’t match any of the Closures, so it would give a 404. Route Names Laravel also allows you to name each route, which enables you to refer to it without explicitly referencing the URL. // app