Error in setting Route Orange 8?

Asked

Viewed 1,271 times

0

I know it might be a beginner question, but I really can’t solve it, and I put the address in the browser the route works, but when I call from blade doesn’t work.

I created a new route and I am unable to access the Blade. Below the code and the error.

web php.

Route::get('logteste', 'App\Http\Controllers\DashboardController@getLogteste');

Dashboardcontroller.php

<?php

namespace App\Http\Controllers;

use App\Repositories\UserRepository;
use App\Validators\UserValidator;

class DashboardController extends Controller
{
    private $repository;
    private $validator;

    public function __construct(UserRepository $repository, UserValidator $validator)
    {
        $this->repository = $repository;
        $this->validator  = $validator;
    }

    public function getLogteste(Request $request)
    {
        echo ('teste');
    }
}

call on the Blade

<span>
    <a href="{{ route('logteste') }}">link</a>
</span>

Error that is presenting

Symfony\Component\Routing\Exception\RouteNotFoundException Route [logteste] not defined. (View:>C:\xampp\htdocs\innovationone\resources\views\templates\menu.blade.php)

  • Try Route::get('logteste', 'App\Http\Controllers\DashboardController@getLogteste')->name('logteste'); .... https://laravel.com/docs/8.x/urls#urls-for-named-Routes

4 answers

2

This problem occurs in question that the route() expects the name of the route and you may be solving in a few ways, a tip first of all, on the routes, in the file web.php, you can put only the name of the controller which will also work, and make your code thinner:

Route::get('logteste', 'DashboardController@getLogteste');`

but before, referring to the main problem, you can pass to flag name() with the name of the route you want, for example:

Route::get('logteste', 'DashboardController@getLogteste')->name('teste');

In his blade would look like this:

<span>
  <a href="{{ route('teste') }}">link</a>
</span>

In case you don’t want to flag name on the route, you can simply use the url() in place of route():

<span>
  <a href="{{ url('logteste') }}">link</a>
</span>
  • In 8 changed the question of 'Dashboardcontroller@getlogteste', you are sure that this is working in 8?

1


On Laravel 8 changed some things on the route, you need to declare it on use web.php:

use App\Http\Controllers\DashboardController;

In this case the statement would be

Route::get('/lista', [DashboardController::class, 'getLogteste'])->name('logteste');

Another option is to do exactly as you did by declaring the full name of the controller within the route, but in this case the route has no name unless you declare it, so just add a name to the route to be able to use in the Blade:

Route::get('logteste', 'App\Http\Controllers\DashboardController@getLogteste')
      ->name('logteste');
  • 1

    Failed to say that - method ->name('logteste') means what it needs to define the name of the route and with that name use in urls

0

Thanks to all I managed to resolve as you suggested

Route::get('/lista', [DashboardController::class, 'getLogteste'])->name('logteste');
  • Do not need to answer what has already been answered by the friends of the answers, just mark one of the answers as a solution to your question

0

There are 3 ways to do this, as Laravel 8 has changed the namespace setting:

  1. Adding $namespace again to the configuration so you can continue using the routes as you did in Laravel 7.x and earlier versions
  2. Use full namespace in your route files when using string syntax
  3. Use action syntax - action syntax (recommended)

Adding the $namespace configuration

This is quite simple. Access the Routesserviceprovider.php file, first insert the following line at the beginning of the class (or remove the comment if it exists like this):

protected $namespace = 'App\Http\Controllers';

Using the full namespace

This change involves changing all your route statements. Although hard work is simple: prefix the names of your controllers with their namespaces. In the following example for an Artigoscontroller class within the app/Http/Controllers folder:

// note como adicionamos o namespace completo antes do nome da classe
Route::get('artigos', 'App\Http\Controllers\ArtigosController@getAll');

Using the action syntax - syntax action

This is the most recently recommended alternative because it is less susceptible to typos and in my experience offers better support for the IDE, as we explicitly inform code which class to use.

Therefore, instead of using the usual string syntax, we can use the action syntax in which we specify the class and method to be used in an array as a parameter of the route method being configured:

// sintaxe de string
Route::get('artigos', 'App\Http\Controllers\ArtigosController@getAll');

// sintaxe ação. 
use App\Http\Controllers\ArtigosController;

Route::get('artigos', [ArtigosController::class, 'getAll']);

For details about these solutions read the article How to fix "Target class [Controller] does not exist" error in Laravel

Browser other questions tagged

You are not signed in. Login or sign up in order to post.