I'm a new user of both Stack Overflow and laravel.
I just started to learn L4 in their official site but now I'm having a small problem in loading the views.
Now my views is not working properly. Any file I call under the views dir results in "file doesn't exist" error.
This is my code ....
Authors controller is:
class AuthorController extends BaseController{
public $restful = true;
public function get_index()
{
return View::make('authors.index');
}
}
On routes.php I added
Route::get('authors',array('uses'=>'authors#index'));
under views/authors/index.php
Any html text
Change
Route::get('authors',array('uses'=>'authors#index'));
to
Route::get('authors',array('uses'=>'AuthorController#get_index'));
If you dont plan on doing complex logic you could skip the controller and just call the view from the router. And also its a good practice to keep the functions as camelCase so in your case getIndex().
Route::get('authors', 'AuthorController#getIndex');
Related
I am trying to add controller view in my existing project that consider model view controller structure in php with laravel.
class CashFlowdataController extends Controller {
public function index() {
return view('CashFlowdata::create');
}
}
When I implement this, it shows me error for,
InvalidArgumentException
No hint path defined for [CashFlowdata].
I have added file in route.php and web.php as added other controller data. Only for this one it shows message like this.
you code is wrong you should to something like this
class CashFlowdataController extends Controller {
public function index() {
return view('CashFlowdata.create');
}
}
here CashFlowdata.create
its means in laravel
folder structure should be
view>CashFlowdata>create.blade.php
laravel view() function is a helper to load view file
ref link https://laravel.com/docs/8.x/helpers#method-view
I had the same issue in nwidart/laravel-modules due to module.json file was miss place.
I move the file to the root of module now working fine.
I have been declaring all the routes for my application inside web.php , but it is now getting quite large. I find that I am losing a lot of time shifting between web.php and each controller and this is hurting productivity.
I feel like it would be better to define routes inside of the controller, perhaps ideally delegating some URL to a controller and then allowing the controller to handle the "sub routes" since this would allow me to use inheritance when I have two similar controllers with similar routes.
It is not possible given how laravel works. Every request is passed onto router to find its designated spot viz. the controller with the method. If it fails to find the route within the router, it just throws the exception. So the request never reaches any controller if the route is not found. It was possible in earlier versions on Symphony where you would configure the route in the comment of a particular controller method.
Sadly with laravel it works how it works.
But for me, I just like to have the routes in a separate file.
Alternate solution, easier way to sort all the routes.
You can move your route registration into controllers if you use static methods for this. The code below is checked in Laravel 7
In web.php
use App\Http\Controllers\MyController;
.....
MyController::registerRoutes('myprefix');
In MyController.php
(I use here additional static methods from the ancestor controller also posted below)
use Illuminate\Support\Facades\Route;
.....
class MyController extends Controller {
......
static public function registerRoutes($prefix)
{
Route::group(['prefix' => $prefix], function () {
Route::any("/foo/{$id}", self::selfRouteName("fooAction"));
Route::resource($prefix, self::selfQualifiedPath());
}
public function fooAction($id)
{
........
}
In Controller.php
class Controller extends BaseController {
....
protected static function selfAction($actionName, $parameters = [], $absolute = false)
{
return action([static::class, $actionName], $parameters, $absolute);
}
protected static function selfQualifiedPath()
{
return "\\".static::class;
}
protected static function selfRouteName($actionName)
{
//classic string syntax return "\\".static::class."#".$actionName;
// using tuple syntax for clarity
return [static::class, $actionName];
}
}
selfAction mentioned here is not related to your question, but mentioned just because it allows making correct urls for actions either by controller itself or any class using it. This approach helps making action-related activity closer to the controller and avoiding manual url-making. I even prefer making specific functions per action, so for example for fooAction
static public function fooActionUrl($id)
{
return self::selfAction('foo', ['id' => $id]);
}
Passing prefix into registerRoutes makes controller even portable in a sense, so allows inserting it into another site with a different prefix in case of conflict
I'm facing one weird problem with my Codeigniter project.
I have two model files in the following paths:
models/public/Dish_model.php
models/admin/Dish_model.php
For front-end and back-end models respectively.
In the controller which is in the following path:
controllers/admin/Dish.php
I'm trying to load the admin area model file using:
$this->load->model('admin/dish_model');
But it is loading the public model file.
Even if I comment this line out the public model file still gets loaded.
This all happened suddenly it was working fine before and I haven't changed any of the mentioned files recently.
Any help?
In case anyone else encounter the same issue.
In my case and after carefully following the execution path of the controller I found that the other model was being loaded by a library in the autoload list.
Removing that library from the autoload array fixed the problem.
to call/load a model from its subdirectory or subfolder.
models/public/Dish_model.php
let's say in Dish_model
class Dish_model extends CI_Model{
public function __construct(){
parent::__construct();
}
public function the_method(){
return 'return value';
}
}
models it's a native ci model directory and public it's a subdirectory. so to load the Dish_model do this on controller
$this->load->model('public/Dish_model','DModel');
the DModel it is like an alias.
so to call the model from the controller is
echo $this->DModel->the_method();
and it will return return value
hope its help.
I'm using codeigniter. I'm currently working on my routes and controller.
Last week, I explored symfony2 and I liked something:
class DefaultController extends Controller
{
public function indexAction()
{
return $this->render('LVIndexBundle:Default:index.html.twig');
}
public function servicesAction()
{
return $this->render('LVIndexBundle:Default:services.html.twig');
}
public function shoppingAction()
{
return $this->render('LVIndexBundle:Default:services.html.twig');
}
In the controller, each action renders a view.
I would like to do the same in codeigniter -> get several functions / actions leading to distinct views.
I'm new to codeigniter. So far, I understood that 1 controller = 1 view.
I'd like to get 1 controller = several functions for several pages. Otherwise, that would be a lot of pages.
Thanks very much for your help!
Based on my experience(s):
In CI, a controller can has more than 1 view php file
ex (function indexAction as a controller function):
public function indexAction()
{
$this->load->view('header');
$this->load->view('content');
$this->load->view('footer');
}
In Symfony and Codeigniter the controllers are just classes that can hold multiple methods. The methods that are called (called Actions in Symfony) are the place that decides which view(s) must be rendered. One of the main differences between Symfony and CI controllers are the routings that Symfony uses. The routes makes Symfony more flexible then CI.
take a look for an explanation of CI controllers
p.s. Symfony is much more advanced then CI. I like to advise you Symfony :-)
Its absolutely not true that there should be one view per controller. even something as simple as validating a form - you will call different views depending on if the validation passes or not.
Strongly suggest you step through the Tutorial in the codeigniter manual. this is going to answer a lot of questions and its very practical.
I recently started using the Laravel framework and I would like the following (but cannot seem to get it right):
pagination, not the kind which Laravel explains but more the kind of /about.html - /portfolio.html etc.
It seems really difficult to achieve this, I searched for a bit and could not find anything or perhaps im not using the right search terms.
The HomeController serves the layout view that has all the html.
The default route is:
Route::get('/', 'HomeController#show');
And this is the HomeControiler:
class HomeController extends BaseController {
public function show() {
return View::make('layout');
}
}
This isn't pagination, it's just more than one route. Your routes for that would be something like:
Route::get('/', 'HomeController#showIndex');
Route::get('/about', 'HomeController#showAbout');
Route::get('/portfolio', 'HomeController#showPortfolio');
The corresponding controller might be like:
class HomeController extends BaseController {
public function showIndex() {
return View::make('index');
}
public function showAbout() {
return View::make('about');
}
public function showPortfolio() {
return View::make('portfolio');
}
}
You definitely don't put the HTML for different routes all in the same view file (shared navigation should be handled via shared layouts and the #extends blade keyword), and it's best not to use the .html extension when routes are perfectly happy without it.