mastercheef85
mastercheef85

Reputation: 2269

Basic Laravel / Programming Questions

I'm trying to structuring my app right and have some trouble with loading class.

So my first question is: the ServiceProviders are just to bind the interfaces, right?

How can i do, that my class is loaded when app is booted.

To be more specific, i try to include the Shortcode logic of Pingpong Sky: http://sky.pingpong-labs.com/docs/2.0/shortcode

What i've done now is to make a folder Shortcodes and put there:

<?php namespace Modules\Account\Shortcodes;

use Shortcode;

class AccountsShortcode
{
   public function register($attr, $content = null, $name = null)
    {
        $text = Shortcode::compile($content);
        return '<div'.HTML::attributes($attr).'>'. $text .'</div>';
     }
}


Shortcode::register('accounts', 'AccountsShortcode');

I tried also to add the folder in the PSR-4 autoload, but it doesn't work.

I alreay have a middleware:

<?php namespace Modules\Page\Http\Middleware;

use Closure;
use Shortcode;

class PageMiddleware
{
    /**
     * Run the request filter.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response   = $next($request);
        $response->setContent(Shortcode::compile($response->original));
        return $response;
    }
}

And this part works.

So, where do i need to put the Shortcode definition code, how can make that it is loaded and what is a good way to structure it?

Upvotes: 1

Views: 155

Answers (2)

patricus
patricus

Reputation: 62228

Based on the namespace you've shown, I'm assuming you're using Pingpong Modules package, as well. If this is the case, after a cursory glance at their documentation, I believe this is what you need to do:

First, create your short code class. Based on the information you've provided, you need to create this class at modules/Account/Shortcodes/AccountsShortcode.php:

<?php

namespace Modules\Account\Shortcodes;

use Shortcode;

class AccountsShortcode
{
   public function register($attr, $content = null, $name = null)
    {
        $text = Shortcode::compile($content);
        return '<div'.HTML::attributes($attr).'>'. $text .'</div>';
     }
}

Next, use your module's service provider to register your shortcode to your shortcode class. In modules/Account/Providers/AccountServiceProvider.php, update the register() method to register your shortcode:

public function register()
{
    // you can add "use" statements at the top if you'd like to clean this up
    \Shortcode::register('accounts', \Modules\Account\Shortcodes\AccountsShortcode::class);
}

Finally, run a composer dump-autoload to make sure the autoloader knows about your new directory and classes.

Upvotes: 1

Arios
Arios

Reputation: 113

Answer one part of your question:

1 - The service provider need to put under the app.php config folder inside your laravel project.

Upvotes: 0

Related Questions