2

I'm building some small cms in laravel and now I need dynamic named methods and variables in my controller based on created page in my cms. I create few pages: news, world, sport, business. and now I need methods and variables in that methods named as pages names, dynamic.

This is my AjaxController.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Post;
use App\Tag;
use App\Page;

class AjaxController extends Controller
{
    protected $posts;
    protected $tags;
    protected $pages;
    protected $methods;



    public function __construct(Post $posts, Page $pages, Tag $tags)
    {
        $this->posts = $posts;
        $this->tags = $tags;
        $this->pages = $pages;

        $this->methods = $this->pages->where('parent_id', null)->where('title', '!=', 'Home')->orderBy('lft', 'asc')->get()->all();

    }

    foreach($this->methods as $method) {

        public function {$method->name}()
        {
            ${$method->name} = $this->posts->with('user')->with('category')->with('subcategory')->where('category_id', $method->id)->orderBy('created_at', 'desc')->paginate(3, ['*'], '{$method->name}');

            return view('ajax.'.$method->name, compact('{$method->name}'))->render();
        }

    }
}

This doesnt work for me.

Results that I need to get should look like this

public function news()
    {
        $news = $this->posts->with('user')->with('category')->with('subcategory')->where('category_id', 8)->orderBy('created_at', 'desc')->paginate(3, ['*'], 'news');

        return view('ajax.news', compact('news'))->render();
    }

public function world()
        {
            $world = $this->posts->with('user')->with('category')->with('subcategory')->where('category_id', 10)->orderBy('created_at', 'desc')->paginate(3, ['*'], 'world');

            return view('ajax.world', compact('world'))->render();
        }


public function sport()
    {
        $sport = $this->posts->with('user')->with('category')->with('subcategory')->where('category_id', 12)->orderBy('created_at', 'desc')->paginate(3, ['*'], 'sport');

        return view('ajax.sport', compact('sport'))->render();
    }

...

Please, how to loop and get dynamic methods and variables in class and parse that dynamic variables in laravel compact and paginate method?

4
  • 1
    Have a look at magic class methods in php. In particular you want to look at the __call magic method. However, these things tend to be against best practice. You should be passing the page identifier to your AJAX request so it can be handled properly. Commented Sep 27, 2016 at 12:21
  • Ajax is here just one example. I need dynamic methods also in some other controllers - php wthout ajax. Commented Sep 27, 2016 at 12:26
  • 1
    Well the __call magic method is specifically designed for this job, but it's slower than standard methods. Commented Sep 27, 2016 at 12:29
  • Can you show me example. I'm not sure how to make query inside __call() magic method and parse variables to views? Commented Sep 27, 2016 at 13:15

1 Answer 1

3

Add a magic call method to your class, you're almost there the following example should be close to what you're thinking:

class AjaxController extends Controller
{

    public function test()
    {
        echo 'Hello world';
    }

    public function __call($name, $arguments)
    {
        $method = $this->pages
            ->where('parent_id', null)
            ->where('title', 'LIKE', $name) // Get method like the name
            ->orderBy('lft', 'asc')
            ->get();

        if (!$method) {
            throw new RuntimeException('Error, page not found');
        }

        ${$method->name} = $this->posts
            ->with('user')
            ->with('category')
            ->with('subcategory')
            ->where('category_id', $method->id)
            ->orderBy('created_at', 'desc')
            ->paginate(3, ['*'], $method->name);

        return view('ajax.' . $method->name, compact($method->name))
            ->render();
    }
}

$test = new AjaxController();
$test->sport(); // this will call the magic __call method with $name = sport
$test->news(); // this will call the magic __call method with $name = news
$test->test(); // this will call the test method
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.