How do you use a Controller within a blade/error.php file? - laravel-8

When I try App::make('App\Http\Controllers\HomeController') I just get server error 500 (and nothing else more helpful)

So now it's just app('App\Http\Controllers\HomeController');
But for most pages I can pass $this into the view from the controller to have access to it on the view

Related

Rails routing to external site from model column through controller

I'm having an issue in my controller. I'm making url objects with original_url short_url and sanitized_url. I can create and save the links just fine. The issue i'm having is when following the short link back to mysite.com/short_url it needs to go through the controller show and grab the sanitized url and redirect to that external site.
Can someone help me figure out what's wrong with this code?
I'm getting undefined method 'sanitized_url'
urls_controller.rb - show
short = params[:short_url]
#url = Url.where("short_url = ? ", short)
redirect_to #url.sanitized_url
My routes.
root to: 'urls#index'
get "/:short_url", to: "urls#show"
get "shortened/:short_url", to: "urls#shortened", as: :shortened
resources :urls
Thank you
undefined method 'sanitized_url'
where returns AR collection. You need to apply sanitized_url on an instance
Below should work
short = params[:short_url]
#url = Url.where("short_url = ? ", short).first
redirect_to #url.sanitized_url

Laravel 5 - Resource Controller in Subfolder

Take a look at this folder structure:
Controllers
-> User
- UserController.php
And this is what i got in my routes.php:
Route::resource('user', 'User\UserController');
What am I doing wrong? I got a 404 error.
If you have your controllers nested try to use underscores instead of backslashes, try this
Route::resource('user', 'User_UserController');

How view sections by id

I have database table called sections (id,name,keywords). I have section.ctp file want show on it the section which its
$id = $this->Session->read('secId');
but i don't know how do this on cakephp . Is it I need to pass variables on sessions? .
in what file I should put sessions vairables
You can pass variable through set method in controller action not using session and you can use it in your view. You can use cake bake command to bake section controller, model and view. For more info how to use cake bake command, please visit here
Hope it helps. (vote and select the answer if it helps)

How do you check if the current page is the frontpage using YII?

Drupal has a function called "drupal_is_front_page". Does YII have something similar to deal with navigation in this way?
Unfortunately not. And while the information needed to piece this together is available, doing so is really more pain than it should be.
To begin with, the front page is defined by the CWebApplication::defaultController property, which can be configured as discussed in the definitive guide. But there's a big issue here: defaultController can in reality be any of the following:
a bare controller name, e.g. site
a module/controller pair, e.g. module/site
a controller/action pair, e.g. site/index
a module/controller/action tuple, e.g. module/site/index
If you have specified the defaultController as #4 (which is the same as #3 if your application does not include any modules) then everything is easy:
function is_home_page() {
$app = Yii::app();
return $app->controller->route == $app->defaultController;
}
The problem is that if defaultController is specified as #1 or #2 then you have to examine a lot of the runtime information to convert it to form #3 or #4 (as appropriate) so that you can then run the equality check.
Yii of course already includes code that can do this: the CWebApplication::createController method, which can accept any of the valid formats for defaultController and resolve that to a controller/action pair (where controller is dependent on the module, if applicable). But looking at the source doesn't make you smile in anticipation.
To sum it up: you can either assume that defaultController will always be fully specified and get the job done with one line of code, or borrow code from createController to determine exactly what defaultController points to (and then use the one line of code to check for equality).
I do not recommend looking into solutions based on URLs because the whole point of routes is that different URLs can point to the same content -- if you go that way, can never be sure that you have the correct result.
In my experience, there is no such function in Yii. However, you can retrieve the followings:
base url: Yii::app()->request->baseUrl
current URL : Yii::app()->request->requestUri.
current page controller with Yii::app()->getController()->getAction()->controller->id .
With these APIs, it should be possible to find out whether the current page is front page.
another simple idea:
in your action (that one you use to present your 'main front page'), you could set up a variable using a script in its view:
Yii::app()->getClientScript()->registerScript("main_screen",
"var main_front_page = true;",CClientScript::POS_BEGIN);
put that code in the "main view", (the rest view pages dont have this piece of code).
so when you need to check if a page is the "main page" you could check for it using javascript, quering for:
if(main_front_page){..do something..}.
if you need to recognize the main page in php (in server side), use the method proposed by Jon.
another solution, based on a common method for your controller:
Your controllers all of them must extend from CController, but, when you build a new fresh yii application Gii creates a base Controller on /protected/components/Controller.php so all your controllers derives from it.
So, put a main attribute on it, named:
<?php
class Controller extends CController {
public $is_main_front_page;
public function setMainFrontPage(){ $this->is_main_front_page = true; }
public function getIsMainFrontPage(){ returns $this->is_main_front_page==true; }
}
?>
well, when you render your main front page action, set up this core varible to true:
<?php
class YoursController extends Controller {
public function actionPrimaryPage(){
$this->setMainFrontPage();
$this->render('primarypage');
}
public function actionSecondaryPage(){
$this->render('secondarypage');
}
}
next, in any view, you could check for it:
<?php // views/yours/primaryview.php
echo "<h1>Main Page</h1>";
echo "is primary ? ".$this->getIsMainFrontPage(); // must say: "is primary ? true"
?>
<?php // views/yours/secondaryview.php
echo "<h1>Secondary Page</h1>";
echo "is primary ? ".$this->getIsMainFrontPage(); // must say: "is primary ? false"
?>

tradeoff routes and views rails 3

here it's routes.db
resources :licenses do
resources :sublicenses do
resources :time_licenses
end
end
Then there is a client application that calls time_licenses_controller for creating new time_licenses, the controller responds with a json file, but i don't need to show any view.
Somewhere else instead i need to show to the client an index file including all time_licenses for every sublicense.
That's the path
license/:id/sublilicense/:id/time_lincenses
Now i have a problem with the routes. When i call the create on time_licenses_controller i get this error:
No route matches "/time_licenses.js"
that i can solve changing the routes.db file like this
resources :time_licenses
resources :licenses do
resources :sublicenses
end
but in that case i get the same error linking the index view.
What do you suggest me? Do i have to create two different controllers?
Since you are using nested resources, you will always need to specify license and sublicense while specifying the path to timelicense.
Your path helpers will be:
license_sublicense_timelicense_path(#license, #sublicense, #timelicense) and so on
You can get the path names for the timelicense by
rake routes
Refer rails guides - nested resources for any doubts.