php
1//in app/config/routes 2Router::parseExtensions('json'); 3 4//in UrlPathController 5<? public function generate_path($url){ 6 $path = $this->UrlPath->generatePath($url); 7 $data = [ 8 'success' => false, 9 'path' => null 10 ]; 11 if($path){ 12 $data['success'] = true; 13 $data['path'] = $path; 14 15 } 16 $this->set('data', $data); 17 $this->set('_serialize', ['data']); 18} 19?> 20 21//in model 22<? 23App::import('Model','UrlPath'); 24public function generatePath($url){ 25 $this->UrlPath= new UrlPath(); 26 $path = substr(str_shuffle('1234567890abcdefghijklmnopqrstuvwxyz'),0,7); 27 $record = $this->UrlPath->findByUrlAndPath($url,$path); 28 if($record==false){ 29 $path = ['path']; 30 31 } 32 return $path; 33 } 34 ?> 35 36//in view 37<?= $this->Form->button('自動生成', ['type'=>'button','id' => 'generate_path', 'class' => 'btn btn-default btn-sm']) ?> 38<?= $this->Form->input('path', ['label' => '短縮URL名','id' =>'path']) ?> 39 40<script> 41$(document).ready(function(){ 42 $(function() { 43 $("#generate_path").click(function() { 44 $.ajax({ 45 url:'<?php echo Router::url(['action' => 'generate_path', 'ext' => 'json']) ?>', 46 type:'post', 47 dataType: 'json', 48 }) 49 .success(function(data) { 50 $("#path").val(data); 51 }) 52 .error(function() { 53 alert('通信エラー'); 54 }); 55 }); 56 }); 57}); 58</script>
今、urlのパスの自動生成ボタンを作成していますが、
logを見ると
Error: Fatal Error (1): Call to a member function findByUrlAndPath() on null というえらがでています。
$record = $this->UrlPath->findByUrlAndPath($url,$path); このラインが間違っているのは合ってるとおもいますが、どうなおせばいいのか分からないです。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2015/08/20 09:51
2015/08/20 14:03
退会済みユーザー
2015/08/21 03:15 編集
2015/08/21 06:09
退会済みユーザー
2015/08/21 07:02
退会済みユーザー
2015/08/21 07:11
2015/08/21 08:25