S
S
Sergey Sergeev2015-01-19 23:26:27
Laravel
Sergey Sergeev, 2015-01-19 23:26:27

How to build a route in Laravel?

Good afternoon, you need to do the elementary - the route on Laravel.
There is a database (id link content), you need to register your own route for each link.
The construction below works only if site.ru/id[c DB] is accessed, but it is necessary that site.ru/link.

Route::model('link', 'Links');

Route::get('/{link}', function (Links $link) {
  return $link;
});

I tried to access the database directly inside the route, it also returns 404, I don’t understand what’s the catch?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Danil Chekalin, 2015-01-19
@alokazay

The model must request data and by some condition

Route::get('/{link}', function ($link) {
  return Links::whereLink($link)->get(); # вернет объект
});

UP#1
Write like this:
Route::get('/{link}', function ($link) {
  return $link;
});

And when you request domain.ru/name-link, you will see "name-link" in the browser. This is the first step. The second is to get data from the model, therefore we write (maybe I'm wrong about the whereLink method, that's why we use it differently)
Route::get('/{link}', function ($link) {
  $data = Links::where('link', '=', $link)->get(); # вернет объект
  var_dump($data); # посмотреть вывод
  return $data;
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question