Laravel Classroom image

Prashant  Nigam / Student / Web Technology

To post your Question Join Classroom
Lesson Topics's No:-  |1 | 2|Last
Lessons:-Links and URLs

Links and URLs
Our application might get a little boring if we only have one page, and I’m sure the user would get
peeved quite quickly if they had to type out the full URI each time to switch pages. Fortunately,
hyper-links are here to save the day.
If you haven’t been living under a rock for the past couple of decades you will already know
what hyper-links are and I won’t bore you with the technical explanation. Before we have a
look at links let’s take a look at how Laravel handles its URLs.
5.1 Retrieving URLs
First let’s take a look at a problem. You see frameworks have very unique URL structures. Some
may have an index.php in them, some installations won’t. Others will have complex routes. In
most cases using a relative URL like you would on another website would lead to trouble in
the long run. If you chose to provide full URL’s to everything and later decided to move the
application to a different domain, you might find yourself abusing the find and replace function
of your favourite editor.
Why not let Laravel do all the hard work? Laravel knows the full URL to your application. It
knows whether or not you are using URL rewriting. It knows about your routes. It even knows
what you keep in that box under your bed.. Let’s take advantage of this information by using
the URL class to generate some site URLs.
Let’s start by finding the URL to the root of our website. We can use the base() method for this.
1 <?php
3 echo URL::base();
4 // http://myproject
Great! Now we have the full URL to our site, with or without the index.php on the end. It all
depends on your current set-up. What about the current URL, the one that is being routed at the
moment, can we get that? You betcha! Simply use the current() method.
1 <?php
3 echo URL::current();
4 // http://myproject/this/page
By default Laravel will strip off the query string if one is appended to the URL. If we want to
retrieve the current URL along with the query string, we can use the full() method instead.
1 <?php
3 echo URL::full();
4 // http://myproject/this/page?thing=stuff
20Knowing our base URL and current URL can be handy, but it would be more useful if we could
get the URL to other routes or pages, then we could create links.
To generate a URL to a route, we use the to() method, and hand it the route that we are trying
to retrieve. This is much easier than specifying the full path, for example..
1 <?php
3 echo URL::to('my/route');
4 // http://myproject/my/route
If we want to link to this page securely via the HTTPS protocol we can use the method to_-
secure() instead.
1 <?php
3 echo URL::to_secure('my/route');
4 // https://myproject/my/route
Do you remember being taught about named routes in the routing chapter? Of course you do!
Here’s an example of one again..
1 <?php
3 Route::get('login', array('as' => 'login', 'do' => function() {
4 // some code
5 }));
Here we have a route that we have named ‘login’ using the ‘as’ array key. Well I told you that
it would be useful later, and now is the time for named routes to shine. Let’s make a link to our
named ‘login’ route..
1 <?php
3 echo URL::to_route('login');
4 // http://myproject/login
Woah! Very clean and expressive I think you will agree. What if we need to supply parameters
to our route? Simple, just pass an array of parameters as a second parameter to the to_route()
method. Let’s imagine for a second that our login route looks more like this…
1 <?php
3 Route::get('my/(:any)/login/(:any)/page')..It’s a terrible route, please don’t use ugly URL’s like this one, but it will help to illustrate a point.
You see, if you pass parameters to the to_route() method, Laravel will automatically work out
which order they should appear in the URL and return the full URL with the parameters in the
right place. Neat!
1 <?php
3 echo URL::to_route('login', array(5, 7));
The above method would give us..
1 http://myproject/my/5/login/7/page
Great! Now our routes will look squeaky clean.. as long as we don’t create routes as complicated
as that one. Also if we decide to change the URI for our route at a later date, we won’t need to
update all of our links!
So that’s routes cleared up, but we shouldn’t forget controllers. No one likes to be left out.
Fortunately there’s a nice and clean way to create a link to a controller action. Simply use the
to_action() method, for example..
1 <?php
3 echo URL::to_action('dashboard@home');
4 // http://myproject/dashboard/home
Just pass the controller name and action, separated by an @ (at) symbol. Once again you can pass
an array of extra parameters as a second parameter to the to_action() method if you need to.
If we are dealing with assets, a CSS style-sheet for example, rather than routes pages we will
need a very different URL. We can’t use URL::to() because that might put an index.php in the
URL or resolve it to one of our routes.
Instead we can use the to_asset() method to generate a correct link. Simply pass the application
relative path to our style-sheet and Laravel will take care of the rest.
1 <?php
3 echo URL::to_asset('css/style.css');
This line will give us..
1 http://myproject/css/style.css
These methods are already very handy, but Laravel takes this a step further by providing shorter
helper methods which look great when used in our views. Here is a list of these helpers, and
their longer alternatives.



Prashant  Nigam

Skills    Laravel

Qualifications :-
Location :-,,,

  Students (0)