Viewed   286 times

I have problem with routes in Laravel, I'm following one tutorial and we have this routes listed in web.php file

Route::get('/home', '[email protected]')->name('home');
Route::get('/blog', '[email protected]')->name('blog');
Route::get('/blog/create', '[email protected]');
Route::post('/blog/store', '[email protected]');
Route::get('/blog/{id}', '[email protected]');
Route::get('/blog/{id}/edit', '[email protected]');
Route::patch('/blog/{id}', '[email protected]');
Route::delete('/blog/{id}', 'BlogContr[email protected]');
Route::get('/blog/bin', '[email protected]');

Problem is in the last route to blog/bin, it is not working if I keep it down below however in tutorial we have moved it to top of other routes and then it's working fine, instructor said that there is some conflict in routes and that that last route need to be at top in order to work but didn't explain at all why ? Can anybody explain in little more details, as I'm really just started with Laravel...

 Answers

2

When accessing a route, Laravel goes through your list of routes top to bottom, until it finds one that 'matches' at which point this route is immediately selected.

In your example, when trying to access /blog/bin using GET, it has two potential matches:

Route::get('/blog/{id}', '[email protected]');

and

Route::get('/blog/bin', '[email protected]');

In this case, Route::get('/blog/{id}', '[email protected]'); comes first so it would be selected.

As the previous answers correctly state, placing the /blog/bin route above the /blog/{id} route would solve the problem. However, this 'solution' leaves you open to a similar mistake in the future (when, for example, defining a /blog/example route and accidentally placing it under /blog/{id}). In addition, I personally think it is not very elegant to have the functioning of your routes depend on the order to place them in.

In my opinion, when possible, a more robust solution is to restrict the possible values that are accepted by /blog/{id} with a regex constraint.

For example, if you are using a numeric ID for your blogposts, you know that you only want to use route /blog/{id} if id is a number. Thus, you would define your route as follows:

Route::get('/blog/{id}', '[email protected]')->where('id', '[0-9]+');

Of course this is often not a possibility, for example if you use the post title as an id, but if there is some way to differentiate a post id from any other /blog/foo route, then this would be a possibility.

Tuesday, November 1, 2022
 
5

The HTTP Kernel is used to process requests that come in through the web (HTTP). Website requests, AJAX, that kind of stuff.

The Console Kernel is used when you interact with your application from the command line. If you use artisan, or when a scheduled job is processed, or when a queued job is processed, all of these actions go through the Console Kernel.

Basically, if you go through index.php, you'll be using the HTTP Kernel. Most everything else will be using the Console Kernel.

Wednesday, October 5, 2022
1

found the answer here on this thread and here . on the second thread Taylor Otwell gives the answer himself . In laravel 5 , the laravel 4 default syntax {{ code }} , will escape the data output . if you want unescaped html data, you have to use the new syntax

{!! HTML::style('css/style.css') !!}

if you want to revert to previous syntax you can use

Blade::setRawTags('{{', '}}');
Friday, August 12, 2022
1

There isn't any built in way to do this. But we have a action helper method which generates route url based on the controller action. We can make use of this and create a simple helper function to achieve the same result. The method also checks if the given controller method is linked to a route, so it does exactly what you need.

function action_exists($action) {
    try {
        action($action);
    } catch (Exception $e) {
        return false;
    }

    return true;
}

// Sample route
Route::get('index', '[email protected]');

$result = action_exists('[email protected]');
// $result is true

$result = action_exists('[email protected]');
// $result is false

You could also verify the existence of the action method using the class directly, but this would return true if the method exists but isn't linked to a route.

Wednesday, August 24, 2022
 
4

You need to group your routes like below

Route::group(['middleware' => 'auth'], function(){

  Route::get('/logout', [
        'uses' => '[email protected]',
        'as' => 'logout'
  ]);


// and your other routes which you wanna protect
}

Now the logout route and other routes which you will add in there will be only accessible to the authenticated users, in simple words user who is logged in.

Thursday, October 27, 2022
 
ayub
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :