C
C
cybernoob2018-02-02 11:23:06
PHP
cybernoob, 2018-02-02 11:23:06

What is the best way to organize classes and methods?

There is a site with articles and the address of the article:
web.com/art/artname
Where art is the section with articles, artname is the url of the article, it is also searched in the database. In the router it looks like this:

$class = $request->$controller.'.controller';
$obj  = new $class;
$act = $obj->showArticle($request->action);

But then another functionality is required, for example, authorization and the address web.com/auth/loginwill be automatically called by auth.controller, but then the problem is because there is no showArticle method in auth.controller, and it is hard-coded in the router.
Options:
1) Increase the length of the URL by the section in which to show the action:
web.com/art/show/artname
web.com/art/edit/artname
web.com/auth/enter/login

Then you can do everything clearly:
$class = $request->$controller.'.controller';
$obj  = new $class;
$act = $obj->$request->action($request->data);

2. Implement one default main method for all classes, for example main and increase the number of classes without increasing sections in the URL, then editing the article will be atweb.com/artedit/artname
$class = $request->$controller.'.controller';
$obj  = new $class;
$act = $obj->main($request->action);

3. Make an if branch in the router, the worst option in my opinion, I don’t want to think about it))
if($controller == 'art'){
    $act = $obj->showArticle($request->action);
}
elseif($controller == 'artedit') {
    $act = $obj->edit($request->action);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin B., 2018-02-02
@cybernoob

Don't you think it's the bottom? Use a normal router and don't use path to controller garbage

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question