Route error not defined in Orange 4

Asked

Viewed 428 times

2

I’m having trouble with a link I created in a view directing to a route. Follow the error:

Errorexception

Route [/user/addUser] not defined
.(View: /var/www/projeto/app/views/principal/index.blade.php)

Routes:

Route::get('/', function() { return View::make('principal.index'); });

Route::get('/user/adduser','Adminusercontroller@adduser');

Controller:

class AdminUserController extends BaseController {


    public function __construct(){
        parent::__construct;
    }

    public function addUser()
    {
        return View::make('user.addUser');
    }

}

View:

@extends('layout.main')

@section('header')

<a href="{{URL::route('/user/addUser')}}"><button class= "btn btn-primary">Novo Cadastro</button></a>

@stop

@section('content')

@stop

@section('footer')

@stop

1 answer

6


Route Names in Laravel 4

One of the biggest mistakes when you start using Laravel 4 is confusing the nome of the route with the url or padrão of the route, see an example:

Route::get('/user/addUser', function(){ //algum codigo });

In the example above, the route has the url or padrão being /user/AddUser but has no name, to be used in Facade URL, we should use it as follows:

URL::to('/user/addUser');

To name a route, we can do it this way:

Route::get('/user/addUser', array(
                              'as' => 'addUserAction', 
                              'uses' => 'AdminUserController@addUser'));

That is, we have a route with padrão /user/addUser and your name (named Routes) is addUserAction, to make a link with the Facade URL, we can now do as follows:

URL::to('/user/addUser');

or

URL::route('addUserAction');
// addUserAction é o nome que escolhi ao definir a rota na chave 'as'

In the example you posted, you are using the padrão or url as if it were the name of the route, if the route has no name, you must use the method URL::to.

  • 1

    TL;DR: For unnamed routes use URL::to('route'); For named routes use URL::route('route name');

Browser other questions tagged

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