Comments (12)
Hi @swimlappy,
Honestly, i've never used the cviebrock/eloquent-sluggable
package, so i've no idea what's your issue.
Can you provide us some code / config / example for test purposes to replicate your issue.
from localization.
Sure. Both packages work fine by themselves using the defaults. There isn't much to the app yet though.
# config/sluggable.php
return [
'build_from' => null,
'save_to' => 'slug',
'max_length' => 245,
'method' => null,
'separator' => '-',
'unique' => true,
'include_trashed' => false,
'on_update' => true,
'reserved' => null,
];
# config/localization.php
return [
'supported-locales' => ['en', 'pt-BR'],
'accept-language-header' => true,
'hide-default-in-url' => false,
'facade' => 'Localization',
'route' => [
'middleware' => [
'localization-session-redirect' => true,
'localization-cookie-redirect' => false,
'localization-redirect' => true,
'localized-routes' => true,
],
],
'locales' => [
'en' => [
'name' => 'English',
'script' => 'Latn',
'dir' => 'ltr',
'native' => 'English',
],
'pt-BR' => [
'name' => 'Brazilian Portuguese',
'script' => 'Latn',
'dir' => 'ltr',
'native' => 'Portuguรชs do Brasil',
],
],
];
# config/app.php
'providers' => [
...
Cviebrock\EloquentSluggable\SluggableServiceProvider::class,
Arcanedev\Localization\LocalizationServiceProvider::class
],
# app/Http/routes.php
Route::group(['prefix' => Localization::setLocale(), 'middleware' => ['localization-session-redirect', 'localization-redirect']], function() {
Route::get('login', ['as' => 'login', 'uses' => 'Auth\AuthController@getLogin']);
Route::post('login', ['as' => 'login_post', 'uses' => 'Auth\AuthController@postLogin']);
Route::get('logout', ['as' => 'logout', 'uses' => 'Auth\AuthController@getLogout']);
Route::group(['middleware' => 'auth'], function () {
Route::get('/', ['as' => 'main', 'uses' => 'MainController@index']);
Route::resource('users', 'UserController');
});
});
# simple user model
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('slug');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
# app/User.php
namespace app;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Cviebrock\EloquentSluggable\SluggableInterface;
use Cviebrock\EloquentSluggable\SluggableTrait;
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract,
SluggableInterface
{
use Authenticatable, Authorizable, CanResetPassword, SluggableTrait;
protected $table = 'users';
protected $fillable = [
'name',
'slug',
'email',
'password'
];
protected $hidden = [
'password',
'remember_token'
];
protected $sluggable = [
'build_from' => 'name'
];
...
# bootstrap/app.php
...
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
app\Exceptions\Handler::class
);
$app->singleton(
'router',
Cviebrock\EloquentSluggable\SluggableRouter::class
);
...
In this setup, I can visit /en/users/1
or /pt-BR/users/1
and localization works. If I try and visit /en/users/my-test-user
or /pt-BR/users/my-test-user
, then I get a NotFoundHttpException
exception thrown. If I comment out the Arcanedev\Localization\LocalizationServiceProvider::class
service provider, and fix my routes, then slugging works fine visiting /users/my-test-user
.
I'm willing to try another slug package if one exists and is known to work.
Thank you for the help.
Jay
from localization.
You're welcome Jay,
You can try this for the time being:
Update your routes.php
from:
Route::group(['prefix' => Localization::setLocale(), 'middleware' => ['localization-session-redirect', 'localization-redirect']], function() {
//...
});
To :
Route::localizedGroup(function () {
//...
});
It's more cleaner and based on your localization.php
config file.
Check also the installation steps and especially this: https://github.com/ARCANEDEV/Localization/wiki/2.-Installation-and-Setup#now-you-need-to-update-your-http-kernel-to-use-the-localization-router
And i'm afraid that i've found a conflict between the two packages :
$app->singleton(
'router',
Cviebrock\EloquentSluggable\SluggableRouter::class
);
with the Localization router: https://github.com/ARCANEDEV/Localization/blob/master/src/Providers/RoutingServiceProvider.php#L53
Did you know what the Cviebrock\EloquentSluggable\SluggableRouter::class
do ??
Try to comment this SluggableRouter
and see if it's fix your issue.
I'm gonna test your case when i've got free time, stay tuned ๐ .
PS: Thanks for using my package.
Youness
from localization.
My app/Http/kernel.php
file did have the proper use statement, left it out of my previous post. Sorry about that. I've updated my routes.php
file with the cleaner group, thank you for that. The Cviebrock\EloquentSluggable\SluggableRouter::class
allows me to use route model binding and route resource with the slug URL. I suppose I could explicitly define my routes, but for this table just seemed faster not to do that. The package doesn't appear to work unless I define that.
Jay
from localization.
Ok i see.
You can try this, go to your app/Providers/RouteServiceProvider.php
and add this inside the boot()
method:
// app/Providers/RouteServiceProvider.php
//...
public function boot(Router $router)
{
parent::boot($router);
$router->model('user', 'App\User', function() {
if (is_null($value)) {
return null;
}
$model = new $class;
$model =$model instanceof SluggableInterface
? $model->findBySlugOrId($value)
: $model->find($value);
if (!is_null($model)) {
return $model;
}
if ($callback instanceof Closure) {
return call_user_func($callback, $value);
}
throw new NotFoundHttpException;
});
}
// ...
Now you can completely remove the :
$app->singleton(
'router',
Cviebrock\EloquentSluggable\SluggableRouter::class
);
i've just copied the content out of it ๐ .
For more details, check the docs : http://laravel.com/docs/5.1/routing#route-model-binding
Check also the namespace of the User
model class, it's App
with capital A not app
.
from localization.
Tinkering with it to get this working. Would I need to do this for every model I want to do route model binding with? Seems like a lot of code to add, when I could normally accomplish it with just 1 line.
from localization.
Nope, this is only to test and solve your issue, this is how it's gonna be (Something like that) :
// app/Providers/RouteServiceProvider.php
//...
public function boot(Router $router)
{
parent::boot($router);
$this->bindModel($router, 'user', 'App\User');
}
protected function bindModel(Router $router, $key, $class, Closure $callback = null)
{
$router->model($key, function ($value) use ($class, $callback) {
if (is_null($value)) {
return null;
}
$model = new $class;
$model =$model instanceof SluggableInterface
? $model->findBySlugOrId($value)
: $model->find($value);
if (!is_null($model)) {
return $model;
}
if ($callback instanceof Closure) {
return call_user_func($callback, $value);
}
throw new NotFoundHttpException;
});
}
// ...
Important:
- If the
$router->model()
is not working properly try$router->bind()
, check SluggableRouter. - I have not test it in a laravel application, i've probably miss a semicolon or something ๐ .
from localization.
Fair enough, I can test for sure. Implementing this at least I don't get any errors :-) But I don't get any data from my model either. Just blank when I visit the slug URL. Hmm
from localization.
As i've said before, i've never used the cviebrock/eloquent-sluggable
package, so i'm probably missing something.
But we are close to fix your issue ( ^_^)b
from localization.
I understand. I think we are close too.
from localization.
Well, some good news. Looks like that worked. I had to add a use statement at the top of my RouteServiceProvider.php
file for use Cviebrock\EloquentSluggable\SluggableInterface;
so that this check would pass
$model = $model instanceof SluggableInterface
? $model->findBySlugOrId($value)
: $model->find($value);
It was returning NULL and thus no data. Thank you for the help! Perhaps this will help someone else looking to do the same thing.
Jay
from localization.
Nice ๐
from localization.
Related Issues (20)
- Translated URL not always working HOT 7
- Problem with language selector with parameters HOT 5
- Symfony\Component\Debug\Exception\FatalThrowableError : Class 'Localization' not found HOT 8
- Current language is not been saved in a session HOT 1
- Laravel 7.0 HOT 1
- Translated urls from a Laravel Package
- do not install with laravel 8 HOT 1
- PHP 8 Support?
- How to get it working with Expose? HOT 2
- Non-static method Arcanedev\Localization\Localization::setLocale() should not be called statically HOT 1
- Route Cache HOT 1
- Question about my language
- Signed routes - 401 Invalid Signature HOT 1
- Bump Laravel 9 HOT 2
- Why don't change of the prefix set a new local/language?
- Localization::localesNavbar() not working in Laravel 9 HOT 1
- JsonSerializable::jsonSerialize() issues on php 8.1
- Laravel 10 support
- Laravel 10 support HOT 1
- mcamara localization not working in 1st reload Laravel
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from localization.