0

I want to know if its possible to create urls dynamically in MVC. Example i am building a shopping store in which i have categories and products. Now if i use product before calling category name or product name it works (http://www.example.com/products/mycategory). But i don't want to use products in the center the URL that i require is http://www.example.com/mycategory.

Same this i want for the content pages.

Can any body help me out on this matter.

tereško
  • 58,060
  • 25
  • 98
  • 150
  • You may use some `htaccess` to redirect `http://www.example.com/mycategory` to `http://www.example.com/products/mycategory` – Kamran Ahmed Oct 14 '14 at 05:07
  • What framework are you using? You want to set up a controller that allows you to harness that end point. – Darren Oct 14 '14 at 05:07
  • I am building my own framework @Darren. Kamran in MVC you don't need to define links in htaccess because every thing is controling in Controllers – Waqar Azeem Oct 14 '14 at 05:19
  • @WaqarAzeem Just set up a controller called `mycategory` with an `index` method which would allow you to simply access `www.mysite.com/mycategory` – Darren Oct 14 '14 at 05:23
  • @Darren thanks for suggetion i will try it – Waqar Azeem Oct 14 '14 at 05:44
  • 1
    @WaqarAzeem you might find this helpful: http://stackoverflow.com/a/19309893/727208 – tereško Oct 14 '14 at 05:57

1 Answers1

1

I'll throw it up in an answer here for you. Basically if you're following MVC structure and all, your controllers are always your endpoints that are visible to the user.

Sine that is the case, all you want to do is create a new controller that matches the term you want to use.

class mycategoryController extends Controller {

    function index() {
        // do your stuff...
    }

}

I can't say exactly how you create your controllers, but that will give you the general gist of things.

Darren
  • 13,050
  • 4
  • 41
  • 79