Calling external controllers, Laravel - php

Having application/controllers/admin/images/ with public function get_upload($id=null) where the controller is set to restful = true
How can I call from my News controller, the function above. I try with the following, but I get 404, but the function its self works... calling by a simple $.post() works (with the full route obviously)
$image = Controller::call('admin.images#upload', array($news_id));
Any ideas how should I do it?
UPDATE
Even with HVMC I still get 404 executing HMVC::get('admin.images#upload', array('news_id'=>$news_id)
applications/controller/admin/images.php
class Admin_Images_Controller extends Admin_Controller
{
public $restful = true;
public function get_upload($news_id)
{
P.S. Admin_Controller exists and its loaded, it has nothing to do with it

What you're looking for is HMVC which Lavarel doesn't support out-of-the-box. I don't use Lavarel so sorry if I get any information wrong, but a quick search directs me here, in which they suggest to use this bundle.
Upon further inspection, seems like you can use Route::forward():
For most cases you can use Route::forward() to achieve what you're trying to do.
Hope it helps :)

I'm experiencing the same problem. The problem is that if the http type (post, get, put, delete) is different it will return 404.
Eg Say you have 2 controllers, image and media. If you have a function called POST_UPLOAD in images controller and another function called GET_RESIZE in media controller, calling GET_RESIZE from POST_UPLOAD will return a 404 and vice versa
But if GET_RESIZE was POST_RESIZE the error would not occur ie same http type.
This means that the http type must be the same.
This is bad because you need to maintain 2 function of RESIZE ie GET_RESIZE & POST_RESIZE.
This is something that is not documented in Laravel.

Related

Codeigniter Front End Controller Not working with libraries

I'm posting this after my hair has been ripped out, ran out of rum, and tried everything I can find on google. I've been developing a site using codeigniter which makes use of templates. I've built the backend first and all is working properly there. So now i've started on getting the front end working which is where I'm hitting the issue.
I've created a controller called pages.php which is going to parse the uri string of the current page, use my library to get the page data from the database, then display it. My pages are all created through an editor on the back end and stored in the database.
So here's the pages controller
class Pages extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library("pages");
}
public function display_page()
{
$page_slug = $this->uri->segment(1);
$data["joes"] = "Here's joes first variable";
$this->pages->get_page($page_slug);
}
}
and here's the error message i get when i hit my url like this demo.mydomain.com/joes-test
and here is how my routes are set up. $route['(:any)'] = 'pages/display_page';
My Pages.php library works perfect on the back end but it's a large file. I've only posted the get_page function below. If you need to see everything let me know. But i dont believe the issue has anything to do with the library itself.
public function get_page($slug){
$objPages = new pages();
$objPages->get_object('slug="'.$slug.'"');
return $objPages;
}
[EDIT] If i place the following inside my homepage controller it works. But the calling function needs to be inside the library.
$this->load->library('pages');
$the_page = $this->pages->get_page("joes-test");
I want to call $this->get_object("joes-test") but this doesn't work. get_object() is an inherited function inside the library.
Now oddly enough. The code i put above will NOT work if i do the exact same thing inside the pages controller
any help leading to a solution would be awesome. I'm under a time crunch and pay to get some assistance. Thanks in advance.
No you can't use the same name with controller and library. please choose another name. for example Mypages for you controller name.
change your routes
$route['(:any)'] = 'mypages/display_page';
then call your controller.
http://demo.mydomain.com/joes-test
I think there nothing wrong with library uri, because as codeigniter official website say: This class is initialized automatically by the system so there is no need to do it manually.
I don't know about lib pages, but how about use
$this->load->view(<file-html>);
and if you want to passing data in variable, you can add variable like this
$this->load->view(<file-html>, $data);
Hope this help, Cheers

Why is Yii2 framework showing a 404 error when creating custom view page?

I'm attempting to create a custom display in yii2 framework using this code in my site controller:
/******/
public function actionChartDisplay()
{
return $this->render('chartDisplay');
}
for testing purposes I pasted the form name in my actionAbout function as a parameter to the render function in it. It worked with this:
public function actionAbout()
{
return $this->render('chartDisplay');
}
But I need to create many custom views in yii2 and this won't be a solution.
This is the error I get
I'm curious as to why it is. Since I was following this tutorial and came across this weird behaviour.
My 'chartDisplay.php' file is merely a "hello world" that does work with the action about function.
in yii2, the controllers and actions with multiple words, that are marked by capital letters are divided by - in your request, so in your case the route would be some/chart-display
Apparently as #SmartCoder pointed out it was an error on how Yii2 Handles the action functions in its controller however I didn't mark his answer as the solution right away because implementing it resulted in an error. So aside from that I'm posting the way I solved it.
So instead of using chart-display I simply changed it for "charts" like this:
public function actionCharts(){
return $this->render('charts');
}
Changed the name of my file so it fits to charts.php and it worked.

Codeigniter RESTful services routing

I am trying to integrate RESTful services to my Codeigniter application. I am using this library https://github.com/chriskacerguis/codeigniter-restserver and the tutorial from https://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter--net-8814.
However, I am a little confused about how to implement routing. The tutorial mentions using the full url but I'd like to do something like:
My Controller
class AdminLogin_WS extends REST_Controller {
public function __construct() {
parent::__construct();
$this->load->model('AccountModel');
}
public function login_get(){
$this->response(json_encode(null));
}
public function login_post(){
$username = $this->post('username');
$this->response(json_encode($username));
}
}
My routes
$route['AdminLogin_WS/Login']['post']= 'AdminLogin_WS/login_post'; <= this will trigger an unknown method error
$route['AdminLogin_WS/Login']= 'AdminLogin_WS/login'; <= this will call the get function
REST Request
public function ws_login(){
$this->curl->create('https://url.com/AdminLogin_WS/Login');
$this->curl->http_login('login','password');
$this->curl->post(array(
'username' => 'auser'
));
$result = $this->curl->execute();
var_dump(json_decode($result));
}
How can I specify what function is a post or get?
You can specify is a function is post or get by using _post() and _get() respectively.
For your routing, I think you are routing wrongly. There should be a difference between the main route and the alternate route. You should have something like
$route['method/param1'] = 'controller/method/param1';
Updated with information from chat
Using login_get() and login_post() and then making the POST request to AdminLogin_WS/login was the correct thing to do, and the login_post() was getting called, there was just some confusion because the POST was returning the same response as the GET using the code that the poster was using.
Original answer
I would post this as a comment but don't have the rep to do so.
What do you mean by "It only works if I create a controller function called login_get()"? That sounds to me like you're sending in a GET rather than a POST to your route. Can you give some information on how you're testing to see if you can POST and get to your login_post()? Have you tried downloading a tool like Postman (https://www.getpostman.com/) and sending in a POST to help eliminate the possibility that you're not sending in the POST correctly?

Form as HMVC widget

Is this the right way to create a form widget in FuelPHP?
class Controller_Widget extends Controller
{
public function action_show()
{
if (Request::is_hmvc())
{
// show form widget
}
else
{
// process form
}
}
}
The form action calls the same function to process, but where will it redirect to after? how will it show validation errors?
Note: The widget should not be accessible through the URL; the form should not display itself if accessed directly through the URL.
EDIT:
Found a similar problem in CodeIgniter HMVC and dynamic widgets but this is from 3 years ago. Maybe the FuelPHP guys have found a better way to do this.
This seems like a weird method, a method that is called show but handles both showing and manipulating data? A method called "show" (or get, fetch, read, etc) shouldn't do any editing, it's name expressly seems to imply that it's a read-only operation.
But also how it proceeds seems off. Its read-operation is HMVC only but its manipulation operation is non-HMVC only? That's really a wrong way to determine what the method should do, whether or not it is HMVC should have no baring on what it does.
In your case I'd split this up into 2 methods: one for retrieval (show()) and another for manipulation (edit() for example). Whether you want to make these HMVC only is up to you. There's more ways than one to solve that, I'd go with:
if ( ! Request::is_hmvc())
{
throw new Exception('Only HMVC access allowed.');
}
Or make it impossible to route to the method by rerouting it in your routes.php config file and then using the HMVC routing overwrite as discussed here: https://stackoverflow.com/a/9957367/727225

How can I tell if a request is made with AJAX in Kohana 3?

I've tried these
request::is_ajax()
Request::instance()->is_ajax
To no avail. I've noticed in the request class there is a public property $is_ajax but I can't seem to be able to access the property.
What am I doing wrong?
in case anyone comes back to this, in Kohana 3.1 it is now $this->request->is_ajax() if you are in a controller.
You could also use this:
if (Request::$is_ajax OR $this->request !== Request::instance())
{ .. }
That way you know that it's an ajax- or ajax-like-request
I use this in my controller base-class so I know whether or not to render the full or partial view.
I ended up getting it to work with Request::$is_ajax
Seems they've gotten rid of the function, and are now relying on a public property.

Categories