lundi 29 mai 2017

laravel controller cannot use currentRouteName() inside construce()

I have named routes:
admin.post.category
admin.post.tag
admin.post.theme

routes\admin.php

Route::group(['prefix' => 'post', 'as' => 'post.'], function () {
    Route::resource('category', 'Admin\Post\TermsController');
    Route::resource('theme', 'Admin\Post\TermsController');
    Route::resource('tag', 'Admin\Post\TermsController');
});

Http\Controller\Admin\Post\TermsController

public function __construct(Request $request)
{
    $this->request = $request;

    $route_name = Route::currentRouteName();
    $arr = explode('.', $route_name);
    echo "<pre>".print_r($arr[2], 1)."</pre>"; exit;

}

When I visit the page http://localhost/admin/post/category, it can show category. But when I run command

php artisan route:list

It shows

  [ErrorException]
  Undefined offset: 2

If I move the three lines from __construct() to index()

$route_name = Route::currentRouteName();
$arr = explode('.', $route_name);
echo "<pre>".print_r($arr[2], 1)."</pre>"; exit;

Then "php artisan route:list" runs well.

How to fix this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire