A
A
Andy Oker2021-12-02 12:08:45
Vue.js
Andy Oker, 2021-12-02 12:08:45

What is the best way to implement transition logic in a router?

We need logic for such paths:

1) https://sitename/city/product/
2) https://sitename/city/
3) https://sitename/product/

Where city and product are dynamic parameters.

What is the best way to handle transitions in the router to these pages, or how would you implement it?

Answer the question

In order to leave comments, you need to log in

8 answer(s)
A
Alexey Yarkov, 2021-12-02
@Ashlis

{
  path: '/city/:product'
},
{
  path: '/:cityOrProduct'
},

It is not clear WHAT kind of logic you need.
UPD: I don't see any other options.

M
Maxim Kuznetsov, 2015-05-12
@max-kuznetsov

I would create an AbstractClass class so that CommonClass and ConcreteClass1 inherit from it. ConcreteClass2 must inherit from ConcreteClass1. In AbstractClass, I would take out from CommonClass those members that should be common to all classes. Everything.

D
D', 2015-05-12
@Denormalization

No way, either inheritance or traits.
The most optimal variant of the trait.

F
Finesse, 2015-05-13
@Finesse

Maybe like this:
common.php:

public function doSomething1() {/*...*/}
public function doSomething2() {/*...*/}

index.php:
class CommonClass {
  public function foo() {/*...*/}
  public function bar() {/*...*/}
}

class ConcreteClass1 extends CommonClass {
  include "common.php";
}

class ConcreteClass2  {
  include "common.php";
  public function doSomething3() {/*...*/}
}

V
Viktor Koltcov, 2015-05-13
@Vityarik

Delegation usually gives more flexibility than inheritance.

class CommonClass {
  public function foo() {/*...*/}
  public function bar() {/*...*/}
}

class ConcreteClass1 extends CommonClass {
  public function doSomething1() {/*...*/}
  public function doSomething2() {/*...*/}
}

class ConcreteClass2  {
  ConcreteClass1 concreteClass1 = new ConcreteClass1();
  public function doSomething1() {concreteClass1.doSomething1()}
  public function doSomething2() {concreteClass1.doSomething2()}
  public function doSomething3() {/*...*/}
}

Try looking at the Strategy pattern.

A
angry_cellophane, 2015-05-15
@angry_cellophane

I support the delegation idea. Another would be to spread the logic across different interfaces

interface Common {
        void foo();
        void bar();
    }

    interface ConcreteWithSomething {
        void doSomething1();
        void doSomething2();
    }

    interface Concrete1 extends Common, ConcreteWithSomething {}

    interface Concrete2 extends ConcreteWithSomething {
        void doSomething3();
    }

    class Concrete2Impl implements Concrete2  {
        @Inject private Concrete1 delegate;

        public void doSomething1() { delegate.doSomething1(); }
        public void doSomething2() { delegate.doSomething2(); }
        public void doSomething3() {/*...*/}
    }

S
Sergey Semenko, 2015-05-12
@abler98

You can just override foo and bar methods in ConcreteClass2

H
He11ion, 2015-05-13
@He11ion

therefore, in ConcreteClass2, the methods foo() and bar() from CommonClass should not be.

Is that why it shouldn't? Option two - either redefine in ConcreteClass1 public function foo() { return null; }
or in ConcreteClass2 something like
public function doSomething1() { 
$cl = new ConcreteClass1(); 
return $cl->doSomething1();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question