Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a namespace so that the class can be easily autoloaded by frameworks like Laravel #5

Open
ssmusoke opened this issue May 21, 2021 · 2 comments

Comments

@ssmusoke
Copy link

No description provided.

@llagerlof
Copy link
Owner

Hi Stephen!

I just made a test using Laravel 8 and the framework autoloaded the class correctly.

My steps:

  • I installed the latest Laravel.
composer create-project laravel/laravel laravel-autoload
  • Inside the laravel-autoload directory I ran:
composer require llagerlof/moodlerest

Tha package was added into composer.json and composer.lock correctly.

  • I opened the web.php and instantiated the class inside the default route:
Route::get('/', function () {
    $MoodleRest = new MoodleRest('http://127.0.0.1/moodle/webservice/rest/server.php', '8f12e614dae30735260a045313caa400');
    dd($MoodleRest);
    return view('welcome');
});

And it worked.

Did you ran composer dump-autoload inside your Laravel project? Sometimes this is necessary to Laravel find the new packages to autoload them.

And about the namespace, the reason I am avoiding it is because this class is aready being used by some people, and I don't want to disrupt something in their projects.

@Art4
Copy link

Art4 commented Oct 4, 2024

And about the namespace, the reason I am avoiding it is because this class is aready being used by some people, and I don't want to disrupt something in their projects.

@llagerlof You might add a namespace to your class and use class_alias() to keep BC.

example:

// in MoodleRest.php

namespace Vendorname\Packagename;

class MoodleRest {
    //...
}

class_alias('Vendorname\Packagename\MoodleRest', 'MoodleRest');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants