GithubHelp home page GithubHelp logo

Comments (12)

arcanedev-maroc avatar arcanedev-maroc commented on June 8, 2024

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.

swimlappy avatar swimlappy commented on June 8, 2024

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.

arcanedev-maroc avatar arcanedev-maroc commented on June 8, 2024

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.

swimlappy avatar swimlappy commented on June 8, 2024

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.

arcanedev-maroc avatar arcanedev-maroc commented on June 8, 2024

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.

swimlappy avatar swimlappy commented on June 8, 2024

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.

arcanedev-maroc avatar arcanedev-maroc commented on June 8, 2024

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.

swimlappy avatar swimlappy commented on June 8, 2024

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.

arcanedev-maroc avatar arcanedev-maroc commented on June 8, 2024

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.

swimlappy avatar swimlappy commented on June 8, 2024

I understand. I think we are close too.

from localization.

swimlappy avatar swimlappy commented on June 8, 2024

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.

arcanedev-maroc avatar arcanedev-maroc commented on June 8, 2024

Nice ๐Ÿ‘

from localization.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.