Jhourlad Estrella
Jhourlad Estrella

Reputation: 3670

Laravel: Route Model Binding and Namespace

I am successfully using the following codes to auto-route HTTP parameters to a model in Laravel:

Route::model('user', 'User', function()
{
    throw new AccountNotFoundException('Account does not exists.');
});

I recently started using namespaces to my classes and that's where the problem started.

So far, I tried this one to no avail:

Route::model('user', 'PackageName\User\Repository\User', ....blahh....)

Question: How do I specify the namespace on Model routes?

Upvotes: 0

Views: 560

Answers (1)

Jhourlad Estrella
Jhourlad Estrella

Reputation: 3670

Analyzing the error message itself, I chanced to solve the problem. Apparently the 2nd parameter of the Route::model() method accepts either a string class name or an object of the model itself:

Route::model('user', new PackageName\User\Repository\User, function()
{
    throw new AccountNotFoundException('Account does not exists.');
});

where:

new PackageName\User\Repository\User

is an instantiation of the namespaced model.

Upvotes: 2

Related Questions