如何在Laravel 4中单击按钮后在jquery中调用控制器函数

时间:2013-07-17 03:47:31

标签: jquery controller laravel laravel-4

我希望从控制器调用一个函数,在单击按钮后将一些数据存储在数据库中。也就是说,在javascript(jquery)中,我有:

$( '.opener' ).click(function() {
        FB.ui({
            method: 'feed',
                // redirect_uri: '/index.php',
            link: 'http://test.com',
            name: 'Bleh',
            caption: 'Blah',
            description: 'Testing!'
            // {{$artist->stage_name}} 
            });

        // Want to call controller function here:
         {{Artists:function}}


      });

在这种情况下如何从控制器调用该功能?在上面的例子中,控制器是ArtistsController,功能是“function”。谢谢。

4 个答案:

答案 0 :(得分:0)

您只需像普通网址一样链接到它 - 就像这样:

$( '.opener' ).click(function() {
        FB.ui({
            method: 'feed',
            link: 'http://test.com/artists/function', // put the link here
            name: 'Bleh',
            caption: 'Blah',
            description: 'Testing!'
            });
      });

在你的routes.php中

Route::get('/artists/function', array('uses' => 'ArtistsController@function'));

答案 1 :(得分:0)

将路线改为

Route::get('artists/function', array('as' => 'artists/function', 'uses' => 'ArtistsController@function'));

答案 2 :(得分:0)

您可以使用函数,例如将数据库类声明为Controller,但您可以直接执行此操作而不使用路径:

<html>
 <body>
  <h1>home.blade.php</h1>
  <?php 
   function test(){ 
    $users = DB::table('users')->join('users_views', 'users.id', '=', 'users_views.id')->where('users_views.id',Auth::user()->id)-‌​>get(); foreach ($users as $permiso) { $ck2 = $permiso->perm; } 
   } 
   test(); 
   echo ";)"; 
  ?>
 </body>
</html>

答案 3 :(得分:0)

<?php   
    function get_fotos_productos($id_prod){
        //here call DB Class 
        foreach ($fotos as $foto)
        {
            $foto='<img src="img/productos/big/'.$foto->foto.'" width="50" alt="">';
            echo  $foto;
        } 

 } ?>

例如在我看来

<ul class="nav navbar-nav navbar-right">
    <li class="dropdown">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> <span class="glyphicon glyphicon-shopping-cart"></span> 7 - Items<span class="caret"></span>
        </a>
        <ul class="dropdown-menu dropdown-cart" role="menu">

        <?php 
        $id_prod=3;
        foreach ($cart as $key => $value) {

            echo '                          
            <li>
                <span class="item">
                    <span class="item-left">
                        '. get_fotos_productos($id_prod).'        
                        <span class="item-info">
                            <span>Codigo :'.$value["code"].'</span>
                            <span>Cantidad : '.$value["qty"].'</span>            
                        </span>
                    </span>
                    <span class="item-right">
                        <button class="btn btn-xs btn-danger pull-right">x</button>
                    </span>
                </span>
            </li>
            ';        

         }
         ?>
         <li class="divider">
         <li><?php echo "Costo Total:".$total; ?></li>
         <li><a class="text-center" href="">View Cart</a></li>
      </ul>

    <!-- end snippet -->