Routing for index doesn't work

Hi everyone, I create new project using phalconPhp and I'de like to do the first test by creating indexcontroller with one method indexAction : public function indexAction() { echo "<h1>Hello!</h1>"; }

when I run my project with localhost/MyProject/ , I can't see my echo. for the routing I have file called router.php which contains :

$di->set('router', function () {
$router = new Router();

$router->add("/:controller", array(
    'controller' => 1,
    'action'     => 'index'
));

$router->add("/:controller/:action", array(
    'controller' => 1,
    'action'     => 2
));

$router->add("/:controller/:action/:params", array(
    'controller' => 1,
    'action'     => 2,
    'params'     => 3
));

return $router;
});

any suggestion ?!



5.3k

Do you have a view? Do you put your echo in the view or in the controller?

To test controllers use:

public function indexAction() 
{ 
  echo "<h1>Hello!</h1>";
  exit();
}


5.3k
edited Dec '15

Create a new folder in the views folder called index and put a new file there called: index.phtml . Put there all your html code.

BTW, you should create a virtual host and make sure rewrite modules are neabled on your http server. Have a look here as well: https://docs.phalconphp.com/en/latest/reference/tutorial.html#file-structure



2.6k

Good morning, Yes I created folder with the same name of controller and a view(.html) with the same name of my action. instead of getting echo into my view I used $this->view->disable(); and my echo to get what I had in controller.



2.6k

The problem is my navigator show thing as I use php normally, it's like there is no routing. when I run my app it shows : Index of /myProject, then a set of my folders (app and public folder)

edited Dec '15

if I understand right:

http://localhost/MyProject/

    // Setup a base URI so that all generated URIs include the "MyProject" folder
    $di->set('url', function () {
        $url = new UrlProvider();
        $url->setBaseUri('/MyProject/');
        return $url;
    });


2.6k

<?php

use Phalcon\Loader;
use Phalcon\Tag;
use Phalcon\Mvc\Url;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Application;
use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\Router;

try {
// Register an autoloader
$loader = new Loader();
$loader->registerNamespaces(
    array(
        'Core\Utils' => '../app/core/utils/',
        'Core\Db' => '../app/core/Db/',
        'App\Model' => '../app/models/'
    )
)->register();
$loader->registerDirs(
    array(
        '../app/controllers/',
        '../app/models/'
    )
)->register();
// Create a DI
$di = new FactoryDefault();

include __DIR__ . "/../app/config/router.php";

// Set the database service
$di['db'] = function() {
    /*return new DbAdapter(array(
        //"host"     => "localhost",
        "host"     => "172.16.17.89",
        "username" => "sa",
        "password" => "sa",
        "dbname"   => "mamdamcma"

    ));*/


return   odbc_connect("Driver={SQL Server};Server=localhost:8082;Database=onpprojet;", "sa", "sa");
};

// Setting up the view component
$di['view'] = function() {
    $view = new View();
    $view->setViewsDir('../app/views/');
    return $view;
};
// Setup a base URI so that all generated URIs include the "tutorial" folder
$di['url'] = function() {
    $url = new Url();
    $url->setBaseUri('/ONPProject/');
    return $url;
};

// Setup the tag helpers
$di['tag'] = function() {
    return new Tag();
};

// Handle the request
$application = new Application($di);

echo $application->handle()->getContent();

} catch (Exception $e) {
 echo "Exception: ", $e->getMessage();
}


2.6k

And for router file : "/../app/config/router.php";

$di->set('router', function () {

$router = new Router();

$router->add("/:controller", array(
    'controller' => 1,
    'action'     => 'index'
));

$router->add("/:controller/:action", array(
    'controller' => 1,
    'action'     => 2
));

$router->add("/:controller/:action/:params", array(
    'controller' => 1,
    'action'     => 2,
    'params'     => 3
));

return $router;
});


2.6k

oscarmolinadev, yes when I write the url normally it has to redirect me to my IndexController and shows "Hello" But instead of, it gives set of my project folder