简体   繁体   中英

How should I use route and view in laravel

I have started to learn laravel. I want to display test view on link click from welcome view, so I used

route method,

Route::get('AGE/',function() {
    return view('test');
});

In my welcome view I used

<a href="http://localhost:8000/AGE">Test</a>

to display test view.

I just want to know the way that I have used to perform required task is a good practice or not. If not please suggest me how to do so? Thanks in advance.

In my opinion, you can write an function in Route for testing only .

In a real project, you must use an Controller in route to working. It's like

Route::get('age',['as' => 'getAge','uses' => 'HomeController@getAge']);

In HomeController

public function getAge(){
     return view('welcome');
}

Hope this helps.

You can use URL::to() which adds http://localhost:8000/ like this:

<a href="{{URL::to('/AGE')}}">Test</a>

Make a controller using artisan command,

php artisan make:controller TestController

Now make a function

public function getTest(){
    return view('test');
}

And, define route like:

 Route::get('/AGE', 'TestController@getTest');

In this way you can do your task.

Include your domain url will make your site not dynamic. Instead use url() . Also the {{ }} is how to display data in blade file

<a href="{{ url('AGE') }}">Test</a>

Take note that always use small case for url. ie /age instead of /AGE

You don't have to hardcode the link. You could use helper methods

<a href="{{ url('/AGE') }}">Test</a>

or provide an alias to your route

Route::get('AGE', function() {
    return view('test');
})->name('test');

and call the named route as

<a href="{{ route('test') }}">Test</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM