2016-04-01 9 views
2

link önce bu kullanmadıysanız: http://altorouter.com/ Ben küçük bir uygulama yapıyorum ama bir çerçeve, sadece yönlendirme parçası gerektirmeyenAltorouter çözemiyorum nasıl rota doğru PHP

. Bu yüzden oldukça basit görünüyordu gibi altorouter denemek için karar verdim.

Belirli yolları yapmak için belirli rotaları eşlemek istiyorum. örneğin:

www.example.com/products/

bu benim products.php şablonu göstermelidir ve alanları doldurmak için veritabanından veri çekin. Aşağıdaki bu çalışma var: rota değiştirebilir zaman

$router->map('GET', '/products', function() { 
    require('partials/connectdb.php'); //Require Database Connection 
    $pageContent = getProductsContent($conn); //prepare all the page content from database 
    require __DIR__ . '/products.php'; //require the template to use 
}); 

diğer standart sayfaların hepsi için aynı, benim sorundur.

www.example.com/shoes/

www.example.com/shorts/

www.example.com/shirts/

www.example.com/ties Örneğin:/

Bir kullanıcı bu rotalara gittiğinde, param 'ayakkabısını' almak istiyorum ve daha sonra hala products.php şablonunu kullanırken, sadece ayakkabılar için mantığı yapın.

Yani dokümanlar bakarak bunu yapabileceğini söylüyor:

www.example.com/[*] // şey anlamına gelir.

Ancak, bunu listelenen rotalarıma ekledikten sonra, kullanıcının ziyaret etmesini istediği başka hiçbir şeyi geçersiz kılar. Ziyaret ettikleri Yani eğer:

www.example.com/products // o

Aslında içeride mantığı bulmadan önce çalışmış gibi:

www.example.com/[*]

Herkes altorouter biliyor ve bana yardım edebilir mi? Aşağıda benim tam sayfa kodu yapıştırın olacaktır:

// Site Router 
$router->map('GET', '/', function() { 
    require('partials/connectdb.php'); //Require Database Connection 
    $pageContent = getHomeContent($conn); 
    require __DIR__ . '/home.php'; 
}); 


$router->map('GET', '/products', function() { 
    require('partials/connectdb.php'); //Require Database Connection 
    $pageContent = getProductsContent($conn); 
    require __DIR__ . '/products.php'; 
}); 


$router->map('GET', '/[*]', function($id) { 
    require('partials/connectdb.php'); //Require Database Connection 


    $test = 'this was a test'; 
    $pageContent = getProductsContent($conn); 
    require __DIR__ . '/products.php'; 
}); 

// match current request url 
$match = $router->match(); 

// call closure or throw 404 status 
if($match && is_callable($match['target'])) { 
    call_user_func_array($match['target'], $match['params']); 
} else { 
    // no route was matched 
    header($_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found'); 
} 

cevap

0

Sizin url olmalıdır

www.example.com/products/

www.example.com/products/shoes

Sonra www.example.com/products/shirts

bunu yapabilirsiniz:

$router->map('GET', '/products/:type', function($type) { 
    require('partials/connectdb.php'); //Require Database Connection 

    if(!isset($type)){ 
    $pageContent = getProductsContent($conn); //prepare all the page content from database 
    require __DIR__ . '/products.php'; //require the template to use 

    if($type == 'shoes'){ 
     //do this 
     echo 'shoes'; 
    } 
    if($type == 'shirts'){ 
     //do that 
     echo 'shirts'; 
    } 
}); 
+0

Bu işe yaramıyor, boş bir sayfa açıyorum –

+0

Hata nedir? – iamkdev

+0

hiçbir hata, sadece boş sayfa, altorouter web sitesinde bakarsanız: türü kullanılabilecek bir şey değil sanırım –

İlgili konular