-
AgBRAT
Junior Member
-
Posts: 25
Threads: 11
Joined: May 2022
Reputation:
0
06-07-2022, 04:06 PM
(This post was last modified: 06-07-2022, 04:16 PM by AgBRAT.)
I am still pretty new to MVC and routing especially but I have managed to get the majority of my site navigable with $routes->setAutoRoute(FALSE). HOWEVER, one of my methods won't load even though the URI appears to be correct.
GET: http://localhost/images/view_image/628460ec47d68 is shown in my browser inspect mode as 200 ok status. The page is still blank though.
Here is the route in question (a part of a group route function):
PHP Code: $routes->get('view_image/(:alphanum)', 'Images\View_Image::index/$1', ['as' => 'view_image']);
Here are my routes, I am using named routes because they made more sense to me.
PHP Code:
Code: /*
* --------------------------------------------------------------------
* Route Definitions
* --------------------------------------------------------------------
*/
// Default Route
$routes->get('/', 'Home::index');
// Login and Registration
$routes->match(['get', 'post'], '/users/register', 'Users::register', ['as' => 'register']);
$routes->match(['get', 'post'], '/users/login', 'Users::login', ['filter' => 'noauth', 'as' => 'login']);
// Admin Routes
$routes->group('/admin', ['filter' => 'auth'], function ($routes)
{
$routes->post('/dashboard', 'admin::dashboard');
});
// Users Routes
$routes->group('users', ['filter' => 'auth'], function ($routes)
{
$routes->get('dashboard', 'Users\Members::index', ['as' => 'member_dashboard']);
$routes->get('profile/(:segment)', 'Users\Members::profile/$1', ['as' => 'member_profile']);
$routes->get('edit_profile/(:segment)', 'Users\Members::edit/$1', ['as' => 'edit_profile']);
});
// Images Routes
$routes->group('images', ['filter' => 'auth'], function ($routes) {
$routes->get('gallery', 'Images\Gallery::index', ['as' => 'gallery']);
$routes->get('manage_uploads', 'Images\Gallery::manage', ['as' => 'manage_uploads']);
$routes->match(['get', 'post'], 'upload', 'Images\Gallery::upload', ['as' => 'upload_image']);
$routes->get('upload', 'Images\Gallery::edit/$1', ['as' => 'edit_upload']);
$routes->match(['get', 'post'], 'upload', 'Images\Gallery::store', ['as' => 'store_upload']);
$routes->get('manage_uploads/(:alphanum)', 'Images\Gallery::delete/$1', ['as' => 'delete_upload']);
$routes->get('view_image/(:alphanum)', 'Images\View_Image::index/$1', ['as' => 'view_image']);
$routes->get('view_image/(:alphanum)', 'Images\View_Image::action/$1', ['as' => 'image_action']);
});
// Logout
$routes->get('logout', 'Users::logout', ['as' =>
'logout']);
And here is the Controller::method/$1 not displaying '\App\Controller\Images\View_Image.php' where I have sent the parameter $viewkey:
PHP Code:
Code: class View_Image extends BaseController
{
protected $imageModel;
protected $actionModel;
protected $commentModel;
protected $subscribeModel;
public function __construct()
{
$this->imageModel = new ImageModel();
$this->actionModel = new ActionModel();
$this->commentModel = new CommentModel();
$this->subscribeModel = new SubscribeModel();
if (session()->get('role') != 0) {
echo 'Access denied';
exit;
}
}
public function index(string $viewkey)
{
$likes = $this->actionModel->getLikes($viewkey);
$dislikes = $this->actionModel->getDislikes($viewkey);
$views = $this->actionModel->getViews($viewkey);
$favorites = $this->actionModel->getFavorites($viewkey);
$singleImage = $this->imageModel->getImage(['viewkey' => $viewkey]);
$data = [];
$data = [
'image' => $singleImage,
'tags' => preg_replace('/-/', ' ', explode(',', $singleImage->tags)),
'title' => $singleImage->title,
'views' => get_num_words($views),
'likePercent' => get_percentage($likes + $dislikes, $likes , 0),
'modified' => get_date_words($singleImage->modified_at, date('Y-m-d H:i:s')),
'likes' => get_num_words($likes),
'dislikes' => get_num_words($dislikes),
'favorites' => get_num_words($favorites),
'comments' => get_num_words($this->commentModel->countComments($viewkey)),
'userLiked' => $this->actionModel->userLiked($viewkey),
'userDisliked' => $this->actionModel->userDisliked($viewkey),
'userFavorited' => $this->actionModel->userFavorited($viewkey),
'userSubscribed'=> $this->subscribeModel->userSubscribed($singleImage->uploader),
'userCommented' => $this->commentModel->userCommented(session()->get('username')),
'imageComments' => $this->commentModel->getComments($viewkey),
'script' => 'js/view_image',
];
echo view('templates/header', $data);
echo view('templates/navigation', $data);
echo view('images/view_image', $data);
echo view('templates/footer', $data);
}
And here is the the part of the view where I route_to the Controller:
PHP Code:
Code: <?php
if (! empty($gallery)) {
foreach ($gallery as $row) {
?>
<div class = 'image-object'>
<a href='<?= route_to('view_image', esc($row->viewkey)); ?>' title='<?= esc($row->title);?>'>
<img src='<?= base_url('/assets/img/uploads/' . $row->filename); ?>' alt='<?= esc($row->title);?>'></a>
-
includebeer
CodeIgniter Team
-
Posts: 1,018
Threads: 18
Joined: Oct 2014
Reputation:
40
A blank page mean there's an error. If it was a routing error, you would get a 404 error. Look at your log files for any error messages.. Also increase your debug level and set your environment to development to see error messages instead of a blank screen. I compiled a list of things to look for when you have troubles with a CI4 app, that may help you with that error.
-
AgBRAT
Junior Member
-
Posts: 25
Threads: 11
Joined: May 2022
Reputation:
0
06-07-2022, 04:19 PM
(This post was last modified: 06-07-2022, 04:24 PM by AgBRAT.)
(06-07-2022, 04:09 PM)includebeer Wrote: A blank page mean there's an error. If it was a routing error, you would get a 404 error. Look at your log files for any error messages.. Also increase your debug level and set your environment to development to see error messages instead of a blank screen. I compiled a list of things to look for when you have troubles with a CI4 app, that may help you with that error. Here is my error log:
Code: CRITICAL - 2022-06-07 18:26:54 --> Call to undefined method App\Controllers\Images\View_Image::default_method()
#0 /Users/nykianderson/Sites/shape_search/vendor/codeigniter4/framework/system/CodeIgniter.php(830): App\Controllers\Images\View_Image->_remap('action', '628460ec47d68')
#1 /Users/nykianderson/Sites/shape_search/vendor/codeigniter4/framework/system/CodeIgniter.php(419): CodeIgniter\CodeIgniter->runController(Object(App\Controllers\Images\View_Image))
#2 /Users/nykianderson/Sites/shape_search/vendor/codeigniter4/framework/system/CodeIgniter.php(326): CodeIgniter\CodeIgniter->handleRequest(NULL, Object(Config\Cache), false)
#3 /Users/nykianderson/Sites/shape_search/public_html/index.php(37): CodeIgniter\CodeIgniter->run()
#4 {main}
INFO - 2022-06-07 18:33:18 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 18:33:20 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 18:34:31 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 18:35:38 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 18:35:39 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 18:35:48 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 18:35:49 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 18:49:21 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 18:52:24 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 18:58:10 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 18:58:12 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 18:59:23 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 18:59:25 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 19:00:57 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 19:00:58 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
(06-07-2022, 04:19 PM)AgBRAT Wrote: (06-07-2022, 04:09 PM)includebeer Wrote: A blank page mean there's an error. If it was a routing error, you would get a 404 error. Look at your log files for any error messages.. Also increase your debug level and set your environment to development to see error messages instead of a blank screen. I compiled a list of things to look for when you have troubles with a CI4 app, that may help you with that error. Here is my error log:
Code: CRITICAL - 2022-06-07 18:26:54 --> Call to undefined method App\Controllers\Images\View_Image::default_method()
#0 /Users/nykianderson/Sites/shape_search/vendor/codeigniter4/framework/system/CodeIgniter.php(830): App\Controllers\Images\View_Image->_remap('action', '628460ec47d68')
#1 /Users/nykianderson/Sites/shape_search/vendor/codeigniter4/framework/system/CodeIgniter.php(419): CodeIgniter\CodeIgniter->runController(Object(App\Controllers\Images\View_Image))
#2 /Users/nykianderson/Sites/shape_search/vendor/codeigniter4/framework/system/CodeIgniter.php(326): CodeIgniter\CodeIgniter->handleRequest(NULL, Object(Config\Cache), false)
#3 /Users/nykianderson/Sites/shape_search/public_html/index.php(37): CodeIgniter\CodeIgniter->run()
#4 {main}
INFO - 2022-06-07 18:33:18 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 18:33:20 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 18:34:31 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 18:35:38 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 18:35:39 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 18:35:48 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 18:35:49 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 18:49:21 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 18:52:24 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 18:58:10 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 18:58:12 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 18:59:23 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 18:59:25 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 19:00:57 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2022-06-07 19:00:58 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
The Critical Error I fixed already. I tried to use a _remap function.
-
includebeer
CodeIgniter Team
-
Posts: 1,018
Threads: 18
Joined: Oct 2014
Reputation:
40
I'm not sure why it's trying to load default_method() but your routes are weird. You have 2 upload and 2 view_image route? How do you expect this to work?
PHP Code: $routes->match(['get', 'post'], 'upload', 'Images\Gallery::upload', ['as' => 'upload_image']); $routes->match(['get', 'post'], 'upload', 'Images\Gallery::store', ['as' => 'store_upload']); $routes->get('view_image/(:alphanum)', 'Images\View_Image::index/$1', ['as' => 'view_image']); $routes->get('view_image/(:alphanum)', 'Images\View_Image::action/$1', ['as' => 'image_action']);
-
AgBRAT
Junior Member
-
Posts: 25
Threads: 11
Joined: May 2022
Reputation:
0
(06-07-2022, 05:15 PM)kenjis Wrote: Remove View_Image::_remap() method.
It is an unnecessary old feature and only makes the existing routing definitions confusing. Okay I removed the remap and the extra view_image route so now the View loads, but how to route for the Ajax requests to like/dislike/favorite/comment/subscribe?
'\App\Views\js\image_action.php' I load my js as a view so that I can pass them dynamically through the controller. It use to work. I send data['script'] = 'js/image_action.php' to the view. Then use:
PHP Code: <?php if (isset($script)) { echo view($script); } ?>
All of this use to work when I allowed set auto route.
Here is my javascript with totally wrong site_url() and no idea how to route them just in case it helps explain how I did it.
PHP Code: <script> $(document).ready(function() { var csrfName = "<?= csrf_token(); ?>"; var csrfHash = "<?= csrf_hash(); ?>"; var viewkey = "<?= esc($image->viewkey); ?>";
// If user views image (on page refresh) $.ajax({ type: "post", url: "<?= site_url('View_Image/action'); ?>", dataType: "json", data: { [csrfName]: csrfHash, "viewkey": viewkey, "action": 'view', }, headers: { 'X-Requested-With': 'XMLHttpRequest', }, success: function(data) { var res = data; csrfName = data.csrfName; csrfHash = data.csrfHash;
// display number of views $('span.views').text(res.views); } });
// if the user clicks on the like button $(".like-button").on("click", function() { var viewkey = $(this).data("viewkey"); $clicked_btn = $(this);
if ($clicked_btn.hasClass("fa-thumbs-o-up")) { action = "like"; } else if ($clicked_btn.hasClass("fa-thumbs-up")) { action = "unlike"; }
$.ajax({ url: "<?= site_url('View_Image/action'); ?>", type: "post", dataType: "json", data: { [csrfName]: csrfHash, "action": action, "viewkey": viewkey, }, headers: { 'X-Requested-With': 'XMLHttpRequest', }, success: function(data) { var res = data; csrfName = data.csrfName; csrfHash = data.csrfHash;
if (action == "like") { $clicked_btn.removeClass("fa-thumbs-o-up"); $clicked_btn.addClass("fa-thumbs-up"); } else if (action == "unlike") { $clicked_btn.removeClass("fa-thumbs-up"); $clicked_btn.addClass("fa-thumbs-o-up"); }
// display number of likes and dislikes $clicked_btn.siblings('span.likes').text(res.likes); $clicked_btn.siblings('span.dislikes').text(res.dislikes);
// Change button styling of the other button if user is reacting the second time to image $clicked_btn.siblings("i.fa-thumbs-down").removeClass("fa-thumbs-down").addClass("fa-thumbs-o-down"); } }); }); // If the user clicks on the dislike button $(".dislike-button").on("click", function() { var viewkey = $(this).data("viewkey"); $clicked_btn = $(this);
if ($clicked_btn.hasClass("fa-thumbs-o-down")) { action = "dislike"; } else if ($clicked_btn.hasClass("fa-thumbs-down")) { action = "undislike"; }
$.ajax({ url: "<?= site_url('View_Image/action'); ?>", type: "post", dataType: "json", data: { [csrfName]: csrfHash, "action": action, "viewkey": viewkey }, headers: { 'X-Requested-With': 'XMLHttpRequest', }, success: function(data) { var res = data; csrfName = data.csrfName; csrfHash = data.csrfHash;
if (action == "dislike") { $clicked_btn.removeClass("fa-thumbs-o-down"); $clicked_btn.addClass("fa-thumbs-down"); } else if (action == "undislike") { $clicked_btn.removeClass("fa-thumbs-down"); $clicked_btn.addClass("fa-thumbs-o-down"); }
// display number of likes and dislikes $clicked_btn.siblings('span.likes').text(res.likes); $clicked_btn.siblings('span.dislikes').text(res.dislikes);
// Change button styling of the other button if user is reacting the second time to image $clicked_btn.siblings("i.fa-thumbs-up").removeClass("fa-thumbs-up").addClass("fa-thumbs-o-up"); } }); }); // if the user clicks on the favorite button $(".favorite-button").on("click", function() { var viewkey = $(this).data("viewkey"); $clicked_btn = $(this);
if ($clicked_btn.hasClass("fa-regular fa-heart")) { action = "favorite"; } else if ($clicked_btn.hasClass("fa-solid fa-heart")) { action = "unfavorite"; }
$.ajax({ url: "<?= site_url('View_Image/action'); ?>", type: "post", dataType: "json", data: { [csrfName]: csrfHash, "action": action, "viewkey": viewkey }, headers: { 'X-Requested-With': 'XMLHttpRequest', }, success: function(data) { var res = data; csrfName = data.csrfName; csrfHash = data.csrfHash;
if (action == "favorite") { $clicked_btn.removeClass("fa-regular fa-heart"); $clicked_btn.addClass("fa-solid fa-heart"); } else if (action == "unfavorite") { $clicked_btn.removeClass("fa-solid fa-heart"); $clicked_btn.addClass("fa-regular fa-heart"); }
// display number of favorites $clicked_btn.siblings('span.favorites').text(res.favorites); } }); });
// If user clicks the subscribe button $(".subscribe").on("click", function () { var userProfile = $(this).data("user"); $clicked_btn = $(this);
if ($clicked_btn.hasClass("fa-solid fa-rss")) { action = "subscribe"; } else if ($clicked_btn.hasClass("fa-solid fa-user-check")) { action = "unsubscribe"; }
$.ajax ({ url: "<?= site_url('Members/subscribe'); ?>", type: "post", dataType: "json", data: { [csrfName]: csrfHash, "action": action, "user_profile": userProfile, }, headers: { 'X-Requested-With': 'XMLHttpRequest', }, success: function(data) { var res = data; csrfName = data.csrfName; csrfHash = data.csrfHash;
if (action == "subscribe") { $clicked_btn.removeClass("fa-solid fa-rss"); $clicked_btn.addClass("fa-solid fa-user-check"); } else if (action == "unsubscribe") { $clicked_btn.removeClass("fa-solid fa-user-check"); $clicked_btn.addClass("fa-solid fa-rss"); } } }); });
$("#cancel-comment-reply-link").hide();
$(".reply-button").on('click', function(event) { event.preventDefault(); var id = $(this).attr("id");
if ($("#li-comment-" + id).find('ul').length > 0) { $("#li-comment-" + id + " ul:first").prepend($("#comment-form-wrapper")); } else { $("#li-comment-" + id).append($("#comment-form-wrapper")); }
$("#reply-id").attr("value", id); $("#cancel-comment-reply-link").show(); });
$("#cancel-comment-reply-link").bind("click", function(event) { event.preventDefault(); $("#reply-id").attr("value", ""); $("#comment-object").prepend($("#comment-form-wrapper")); $(this).hide(); });
$("#comment-form").bind("submit", function(event) { event.preventDefault();
if ($("#comment-text").val() == "") { alert("Please enter your comment"); return false; }
$.ajax({ type: "post", url: "<?= site_url('View_Image/action'); ?>", data: $('#comment-form').serialize() + "&" + [csrfName] + "=" + csrfHash, dataType: 'html', headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') }, beforeSend: function () { $('#comment-object').block({ message: "Please wait...", css: { border: 'none', padding: '15px', backgroundColor: '#ccc', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', }, overlayCSS: { backgroundColor: '#ffe' }, }); }, success: function (comment) { var replyID = $("#reply-id").val();
if (replyID == "") { $("#comment-object ul:first").prepend(comment);
if (comment.toLowerCase().indexOf("error") >= 0) { $("#comment-resp-err").attr("value", comment); } } else {
if ($("#li-comment-" + replyID).find('ul').length > 0) { $("#li-comment-" + replyID).append('<ul class="comment-text">' + comment + '</ul>'); } } $("#comment-text").attr("value", ""); $("#reply-id").attr("value", ""); $("#cancel-comment-reply-link").hide(); $("#comment-object").prepend($("#comment-form-wrapper")); $("#comment-object").unblock(); } }); }); }); </script>
-
AgBRAT
Junior Member
-
Posts: 25
Threads: 11
Joined: May 2022
Reputation:
0
06-12-2022, 09:18 AM
(This post was last modified: 06-12-2022, 09:19 AM by AgBRAT.)
PHP Code: /* * -------------------------------------------------------------------- * Router Setup * -------------------------------------------------------------------- */ $routes->setDefaultNamespace('App\Controllers'); $routes->setDefaultController('Home'); $routes->setDefaultMethod('index'); $routes->setTranslateURIDashes(TRUE); $routes->set404Override(); $routes->setAutoRoute(FALSE);
/* * -------------------------------------------------------------------- * Route Definitions * -------------------------------------------------------------------- */
// Default Route $routes->get('/', 'Home::index');
$routes->get('pages/(:segment)', 'Home::view_pages/$1', ['pages']);
// Login and Registration $routes->match(['get', 'post'], 'users/register', 'Users::register', ['filter' => 'noauth', 'as' => 'register']); $routes->match(['get', 'post'], 'users/login', 'Users::login', ['filter' => 'noauth', 'as' => 'login']);
// Admin Routes $routes->group('users/admin', ['filter' => 'auth'], function ($routes) { $routes->post('/dashboard', 'admin::dashboard'); });
// Users Routes $routes->group('users/members', ['filter' => 'auth'], function ($routes) { $routes->get('dashboard', 'Users\Members::index', ['as' => 'member_dashboard']); $routes->get('profile/(:segment)', 'Users\Members::profile/$1', ['as' => 'member_profile']); //$routes->get('edit_profile/(:segment)', 'Users\Members::edit_profile/$1', ['as' => 'edit_profile']); //$routes->get('manage_uploads/(:alphanum)', 'Users\Members::manage_uploads/$1', ['as' => 'manage_uploads']); //$routes->post('upload/(:segment)', 'Users\Members::upload_image/$1', ['as' => 'upload_image']); });
// Images Routes $routes->group('images', ['filter' => 'auth'], function ($routes) { $routes->get('gallery', 'Images\Gallery::index', ['as' => 'gallery']); $routes->match(['get', 'post'], 'view_image/(:alphanum)', 'Images\Gallery::view_image/$1', ['as' => 'view_image']); });
// Logout $routes->get('logout', 'Users::logout', ['as' => 'logout']);
When I uncomment the lines the rest of my site breaks besides the default home page:
PHP Code: //$routes->get('edit_profile/(:segment)', 'Users\Members::edit_profile/$1', ['as' => 'edit_profile']); //$routes->get('manage_uploads/(:alphanum)', 'Users\Members::manage_uploads/$1', ['as' => 'manage_uploads']); //$routes->post('upload/(:segment)', 'Users\Members::upload_image/$1', ['as' => 'upload_image']);
|