Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Brian van Vlymen
12,637 Pointsneed help URL relative with blade.php
Hello I am trying to link but it s failed with Laravel MVC. What I put <li><a href=" registration.blade.php ">Log in</a></li> On Hello.blade.php I am trying to click Log in and it said whoops it went something wrong and the url was http://localhost/laravel/ch.1IntegratingFrontEndComponentsWebApp/public/registration
Am I am suppose to put <?php # ?> inside the href? but I got an error from that.
I am lost.
I got mine working two URL's http://localhost/laravel/ch.1IntegratingFrontEndComponentsWebApp/public/register and http://localhost/laravel/ch.1IntegratingFrontEndComponentsWebApp/public/
Here's my routes.php. Is there something wrong with it?
Route::get('/', function()
{
return
View::make('hello');
});
6 Answers
thomascawthorn
22,986 PointsHey man,
Your routes look fine but there might be an issue with your blade file. You shouldn't need to do php tags for a link_to_route function :) Just surrounded in {{ }} is fine.
Brian van Vlymen
12,637 PointsHI, Tom Cawthorn
I found an article similarly to this. It sounds like you probably want something like:
Route::get('register', function(){
View::make('registration');
});
then in your <li> tag, you'd use the href of "/register" note that your routes file determines what URLs will work in your application Route::get('register'... is telling Laravel to listen for and route traffic to the url http://yourapp.com/register the view::make('registration') part is telling Laravel to use the view file registration.blade.php
can do surrounded in {{ }} instead of php tags? For example <a href={{ $name }} is that possible?
thomascawthorn
22,986 PointsLaravel goes one step further - if you have routes set up correctly, you shouldn't need to use any a-tags. Here's what I mean:
In your routes file, you can set up something called a 'named route'. It doesn't have to be named, but this means you can easily call routes from your views.
e.g.
Route::get('home', [
'as' => 'register_path',
'uses' => 'PagesController@home'
]);
This route will call the home method in the PagesController. You can create/use any controller and method you like here :-) In the method you call, you should return your view.
Then, In your view, you can do:
{{ link_to_route('register_path', 'Link Text Goes Here', null, ['class' => 'btn'] }}
The function link_to_route will output a string of html - you don't have to do any html writing for this link. You can find link_to_route on this page by using cmd F (control on PC).
Having all this set up correctly will enable you to say www.myapp,com/home, will route to the PagesController method where the view 'home' can be returned.
I hope this makes sense, let me know if you need more info!
Brian van Vlymen
12,637 PointsDo you have more tutorial for that? honestly, I do not understand at all. I have read it five time already. I reviewed myself on video took my note. I understood is blade syntax basic {{ }} is NOT escape html special characters that will pop up executed with window.alert if its very true statement so use {{{ }}} is to prevent pop up.
I am not sure how and where should I put link_to_route like you said. I was thinking will it work if using with my hello.blade.php.
I have not learned related to helper functions. I probably looking for helper functions out there. Do you have any excellent example or tutorials ?
Brian van Vlymen
12,637 PointsI got mine working what did I put the codes. Is seems fine ? Should I still need to add link_to_routes??? What will you do? app/views/layout.blade.php
<li><a href="register">Register and Login</a></li>
<li><a href="sign-up">Another action</a></li>
app/views/routes.php
Route::get('sign-up', function()
{
return
View::make('signup');
});
Route::get('register', function()
{
return
View::make('registration');
});
Now you gave me the example app/views/routes.php
Route::get('home', [
'as' => 'register_path',
'uses' => 'PagesController@home'
]);
app/views/home.blade.php Is this correct blade???
{{ link_to_route('register_path', 'Link Text Goes Here', null, ['class' => 'btn'] }}
{{link_to_route('route.name', $title, $parameters = array(), $attributes = array())}}
thomascawthorn
22,986 PointsIf you're on the laravel beginners course, you can learn more from here to get you started on RESTful routing. I think you'll need this info to use link_to_route :-)