Download the PHP package mmedia/classcontroller without Composer

On this page you can find all versions of the php package mmedia/classcontroller. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package classcontroller

Laravel ClassController

Latest Version on Packagist Total Downloads Maintained Coverage

The ClassController extends the basic Controller and allows you to use defined PHP class methods directly as controller methods.

Installation

You can install the package via composer:

Usage

See the Test::class that this example is using.

When you extend a ClassController and give your new controller a name like {inheritedClass}ClassController, all of the methods of {inheritedClass} are inherited and wrapped with Laravel validation rules and responses. If you need a namespaced class, you can use inheritedClass property to specify a class with its namespace instead.

In your routes, you can now call all the methods of the inheritedClass, Test::class in this case, directly:

Here is the equivalent when extending the default Controller instead In a `ClassController`, all this code is auto handled for you!

Defining parameters to pass to the inheritedClass constructor

Sometimes a class requires some parameters in its constructor. To define these parameters, your controller should implement the classParameters method, and return the required parameters in the correct order as an iterable.

Further setting up the class after instantiation

If you need to do more on the class before a method is called but after it is set up and instantiated, you can implement the postClassSetup method, where you have access to the instance of your class as $this->class(). This method should not return anything.

Overriding a specific method

If you need to override the behaviour of a specific method, you can simply define it in your controller using the method name that you want to override. Using the original example of Test::class:

Note that while you can write your own validation logic, here we chose to use the already existing method getValidatedData() that is provided by the ValidatesClassMethods trait - the method takes a class name and method name as parameters and then validates all the required method parameters.

Creating a ClassController with Laravel Artisan

First, make sure you publish the stubs with the following command below:

You can then use the Artisan command to create ClassControllers whenever you want. Specify the ---type=class option, followed by the class name you are inheriting, to create a new ClassController:

In detail

Responses

The ClassController will respond differently depending on your headers. If you pass the Accept:application/json header, the responses will be in JSON. If not, you will be redirected back to the previous page with either a validation error if one is thrown, or a success with the result of the method called.

Validation

Validation rules are built using the class method parameters. For example, a function test($param1, $param2) will build a validator that requires param1 and param2 to be passed in the request.

If the parameter has a default value, for example test($param1 = "defaultValue"), the validation rule for param1 will be nullable.

If the parameter is typed, for example test(int $param1), the validation rules for param1 will be required and integer.

If the parameter is variadic, for example test(...$param1), the validation rules for param1 will be required and array.

If the parameter is variadic and typed, for example test(int ...$param1), the validation rules for param1 will be required and array, and each element will also have a validation rule of type integer.

Exceptions

Validation exceptions

Validation exceptions will be handled natively by Laravel validator, and will return the errors in the errors key with a status code of 422. As an example, a validation exception may return something like:

Remember, you must pass the accept header in order to get JSON data back.

Method exceptions

If the called method throws an exception, it will be caught by the ClassController. If you have specified that you accept JSON in the response, the message will be returned as JSON with the key-value of {"message": "Error message"}, and a status code of 400. Otherwise, the Laravel 400 page will be shown.

ClassController exceptions

Exceptions may be thrown by the ClassController itself, especially if its not set up properly. As an example: if the inheritedClass could not be determined, you will receive a native PHP error with a clear message and a status code of 500.

All methods

classParameters(): iterable

Parameters passed to the inherited class constructor. See: defining parameters to pass to the class.

postClassSetup(): void

Method that runs after the class is instantiated. See: further setting up the class after instantiation.

getValidatedData($className, $methodName): array

Method that takes the name of a method and a class, looks through the method parameters, generates a Laravel validation instance and validates it using Laravel rules. Returns an array of valid data, or throws a See: validation.

class(): object

Get an instance of the inherited class. The actual instance of the class, already instantiated.

All properties

inheritedClass

A protected string ('MyClass') or class (MyClass::class). This will be the class that the methods are inherited from.

If you do not define this property and your controller follows the naming standard of {inheritedClass}ClassController extends ClassController, the inheritedClass will be taken from your controllers name.

ValidatesClassMethods trait

If you want to use the validator method but don't want to extend your controller with the ClassController class (or want to use the validation logic outside of a controller), you can use the ValidatesClassMethods trait in your code.

Limitations

Package development

Running

You can use the included VSCode devcontainer to develop within a PHP container.

Testing

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.


All versions of classcontroller with dependencies

PHP Build Version
Package Version
Requires php Version ^7.4|^8.0
illuminate/support Version ^8.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package mmedia/classcontroller contains the following files

Loading the files please wait ....