1

I am trying to print the current route in my controller

 namespace findetrip\Http\Controllers;

 use Illuminate\Http\Request;

    class HomeController extends Controller
    {
        /**
         * Create a new controller instance.
         *
         * @return void
         */
        public function __construct()
        {
            $this->middleware('auth');
        }

        /**
         * Show the application dashboard.
         *
         * @return \Illuminate\Http\Response
         */
        public function index($page = 'home')
        {
            echo $route = Route::current();
            return view('admin.'.$page,['pageName'=>$page]);
        }

    }

But I got the following error:

Class 'findetrip\Http\Controllers\Route' not found

I found many questions similar to this issue, but didn't get a proper solution.

4
  • Is this a class or just a function? Commented Jan 2, 2017 at 11:01
  • @KhorneHoly its a class Commented Jan 2, 2017 at 11:02
  • You are trying to access class Route but you have not included it in your namespace, so your script can't find it. Either use full url or include the namespace. PS: An IDE like PhpStorm will do this for you automatically. Commented Jan 2, 2017 at 11:26
  • 1
    \Route if you don't want to add use (it will use facade from alias), if you would call something dynamic on some Route instance, you would want to instead add use use Illuminate\Routing\Route; Commented Feb 19, 2019 at 8:46

4 Answers 4

9

To use Route::current(), you have to use Route like:

use Illuminate\Routing\Route;

Note: Look at your app.php, you should have this on 'aliases' array:

'Route' => "Illuminate\Support\Facades\Route",
Sign up to request clarification or add additional context in comments.

2 Comments

Got this error Call to undefined method Illuminate\Routing\Route::current() but this import use Illuminate\Support\Facades\Route; works fine
@BlessanKurien That just means, that your class Route has no method current. You might have included the wrong Route class
2
use Illuminate\Routing\Controller;

Use these code in the below of your controller and try it.

Comments

1

To use Route::current(), you have to include Route class in your controller:

use Route;

Comments

1

You're getting this error because the Route facade isn't imported inside your code. You need to import it by using the following statement:

use Illuminate\Support\Facades\Route;

2 Comments

Would you like to explain your answer? It helps newbie to better understand
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.