Giter Site home page Giter Site logo

URL rewrite about mini HOT 8 CLOSED

panique avatar panique commented on May 21, 2024
URL rewrite

from mini.

Comments (8)

panique avatar panique commented on May 21, 2024

Sorry I really don't know what you want to say :(

from mini.

macagoraga avatar macagoraga commented on May 21, 2024

I mean create a routing like Slim Framework (and many others):

get('/hello/:name', function ($name) { echo "Hello, $name"; }); $app->run();

from mini.

panique avatar panique commented on May 21, 2024

Hmm I think people who understand routes and know what REST is definitly dont need php-mvc ;)
I totally understand your point, but this project might not be the right place for such a feature.
Let's see what others say.

from mini.

macagoraga avatar macagoraga commented on May 21, 2024

hmm....

As I know MVC is how the structure of your app works.

REST is how your app "talks" with other apps.

You can combine them.

A lot of modern frameworks actually are MVC based and make implementing REST web services easy.

But I can understand that you want keep it lightway, a backbone.

I just ask if someone else has already integrated something into php-mvc.

Thanks!!

from mini.

macagoraga avatar macagoraga commented on May 21, 2024

I have implement it :) working fine

from mini.

mituriu avatar mituriu commented on May 21, 2024

Hello @macagoraga may I ask how you implemented the routing? I would like to include it in my application.

from mini.

macagoraga avatar macagoraga commented on May 21, 2024

Hi @mituriu

I will show below example of code Application.php which you can adapt to your code.

I use this php router class (but there are also other which work fine, example you can use Simfony2 Routing):

http://altorouter.com/

find here example of usage:
https://github.com/dannyvankooten/AltoRouter/blob/master/examples/basic/index.php

As you can see you can map class controller, not only anonymous functions.

Example of usage:

<?php

/**
 * Application class
 *
 * Main class for load application
 *
 * @package Library
 */

class Application {

    public $controller = null;
    private $action = null;
    private $params = array();
    private $name = null;

    public function __construct(){}

    /**
     * Start application
     */

    public function start()
    {
      Session\start_session();
      $this->loadRoutes();
      $this->loadController();
    }

    /**
     * Load router
     */

    public function loadRoutes(){

      $router = Service\get('ROUTER');
      $config = Service\get('CONFIGURATION');

      $router->setBasePath($config->get('base_folder'));
      $routes = include BASEDIR . '/private/config/routes.php';
      foreach($routes as $key => $route){
        $router->map($route['methods'], $route['url'], $route['action'], $key);
      }
      $match = $router->match();

      if(is_array($match)) {
        $this->controller = $match['target']['c'];
        $this->action = $match['target']['a'];
        $this->params = $match['params'];
        $this->name = $match['name'];

      } else {

        // NOT FOUND
      }

    }


    /**
     * Execute controller code
     */

    private function loadController() {

      if (! $this->isController($this->controller)) {
        // ERROR :: Is Not a Controller
      }

      // Create new instance of controller
      $object = new $this->controller();
      // Prepare params
      $params = array_values($this->params);
      // Prepare callback
      $callback = [$object, $this->action];
      // Check if method exist
      if (method_exists($object, $this->action) && is_callable($callback)) {
        // Execute matched controller method
        call_user_func_array($callback, $params);
      }
      // Check if index method exist
      elseif (method_exists($object, 'index') && is_callable([$object, 'index'])) {
        // Execute matched controller method index()
        call_user_func_array([$object, 'index'], $params);
      }

      // Not found controller
      else {
        // Display error 404 page not found

      }
    }


    /**
     * Check if a controller exist
     */

    private function isController($controller) {
      return (is_readable(BASEDIR . '/private/controller/' .strtolower(str_replace('\\', '/', $controller)).'.php'));
    }

} // end Application class


routes.php file it's like below:

<?php return [


// ACCOUNT
'unique-route-name' => [
  'methods' => 'GET',
  'url' => '/my-custom-url',
  'action' =>
  [
    'c' => 'MyControllerClassName',
    'a' => 'myMethodName',
  ]
],
'unique-route-name-2' => [
  'methods' => 'GET',
  'url' => '/my-custom-url',
  'action' =>
  [
    'c' => 'MyControllerClassName2',
    'a' => 'myMethodName2',
  ]
],

...

This is my custom way to map routes, you can create your own.

Hope this help.

from mini.

mituriu avatar mituriu commented on May 21, 2024

Hi @macagoraga, Thank you very much for the long explanation. I will give this a try and let you know how it goes.

from mini.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.