Laravel route throwing: cannot find localhost - php

I'm trying to understand RESTfull controllers and Laravel 3 routing. I created a restfull controller Articles and now want to create the following methods:
GET: index
GET: write // (new but new is reserved)
GET: edit
POST: create
PUT: update
DELETE: destroy
However before I even started I keep getting redirected when pressing New article link in my view that should redirect to articles/write instead I get redirected to blank page with chrome error: Chrome can't find localhost.
My controller:
<?php
class Articles_Controller extends Base_Controller {
public $restful = true;
public function get_index()
{
return View::make('articles.index', array('articles' => Article::all()));
}
public function get_write()
{
return View::make('articles.new');
}
public function post_create()
{
return 'Created';
}
}
My routes:
?php
Route::get('/', 'home#index');
// Articles
Route::controller('articles');
My view:
<h1>Todays articles:</h1>
<?php
if(sizeof($articles) == 0)
{
echo 'No articles published';
}
?>
<br /><br />
<?= HTML::Link('articles/write', 'New article') ?>

Try using
HTML::link_to_action('articles#write', 'New article');
and set up your root URL right in the application/config/app.php to http://localhost or http://127.0.0.1!

Related

Routing Error when trying to access a linkother than the baseURL

I have a "routing" problem on CodeIgniter 4.2.7.
My home page is displayed well, the page also reloads well when I click on "home" in my menu, on this side there is no problem, however when I want to access another page I have the message error following "Error 404".
Here is my controller:
class c_accueil extends BaseController
{
public function index()
{
$data['titre'] = "Accueil";
return
view('v_menu')
. view('v_accueil',$data)
. view('v_footer');
}
public function espaceNintendo()
{
$data['titre'] = "Espace Nintendo";
return
view('v_menu')
. view('v_espaceNintendo', $data)
. view('v_footer');
}
Here is my base url:
public $baseURL = 'http://localhost:63342/ProjetWeb';
Here is my route:
$routes->get('/', 'c_accueil::index');$routes->get('/public/espaceNintendo', 'c_accueil::espaceNintendo');
Here is my view (v_menu) :
<?=anchor(base_url().'/public/', 'Accueil')?>
<?=anchor(base_url().'/public/espaceNintendo', 'Espace Nintendo')?>
So "Accueil" works fine but not "espaceNintendo"
Url of my home page which works well:
http://localhost:63342/ProjetWeb/public/
However, the URL as soon as I click on "Espace nintendo" is this:
http://localhost:63342/ProjetWeb/public/espaceNintendo
But it gives me "error 404".
Do you have any idea where the problem comes from?
My helpers are well loaded on my base_controller:
protected $helpers = ['html', 'form', 'url'];

Undefined variable in Laravel 8

I have issue with the undefined variable in Laravel 8, I actually don't understand why this happen because I was trying and playing with the Laravel 8.
I created the same method and same coding but somehow when I tried to run the coding for page about I got the error that said
ErrorException
Undefined variable: about (View: D:\Server\htdocs\app\resources\views\pages\about.blade.php)
Why is this happening ? I don't understand. Because I use the exact same coding for my other pages and it works but when I try to open the about page it suddenly give me the error when other pages are perfectly fine with no error.
PagesController
class PagesController extends Controller
{
public function index()
{
$title = "Welcome to my blog";
// return view ('pages.index', compact('title')); // first method
return view ('pages.index')->with('title',$title); // 2 method
}
public function about()
{
$about = "About Page";
return view ('pages.about')->with('about',$about);
}
public function services()
{
$data = array(
'title' =>'Services' // array
);
return view ('pages.service')-> with($data);
}
}
about.blade.php
#extends('layouts.app')
#section('content')
<h1>{{$about}} </h1>
<p> This is about pages </p>
#endsection
index.blade.php
#extends('layouts.app')
#section('content')
<h1>{{$title}} </h1>
<p> This is tutorial </p>
#endsection
Just to show you the same coding i use for index and about
My route if anyone asking
Route::get('/', [PagesController::class,'index']);
Route::get('/about', [PagesController::class,'about']);
Route::get('/services', [PagesController::class,'services']);
index.blade.php
about.blade.php
public function about ()
{
$ about = "About Page";
return view ('pages.about') -> compact ('about');
}
Replace it as is and try it: your error will be solved.
php artisan route:cache
Clearing the route cache helped me.
My variable was added later and data was output from the cache
for people still suffering from this issue , head over to web.php and make sure you dont have a duplicated route.

Codeigniter 3 Routes and Handling 404 Errors

I am trying to configure pretty URLs in codeigniter and it's not making much sense to me. I'd like my URLs to follow the following structure;
example.com/admin/view/form/123
I can successfully view data when I visit the URL above. I see the same data when I visit;
example.com/admin/view/123
Notice the 3rd segment /form/ is missing, but still returns the data. It's almost like it's ignoring this - I thought CI should throw a 404 error, or do I need to check for this manually? If so, how?
When I visit this URL;
example.com/admin/view
I see the following error;
An uncaught Exception was encountered
Type: ArgumentCountError
Message: Too few arguments to function Admin::view(), 0 passed
My code can be seen below;
Controller
defined('BASEPATH') OR exit('No direct script access allowed');
class Admin extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->database();
$this->load->model('Admin_model');
}
public function index()
{
$this->load->view('template/header');
$data = array(
'title' => 'Admin Page'
);
$this->load->view('admin/index', $data);
$this->load->view('template/footer');
}
public function view($form_submission_id)
{
$this->load->view('template/header');
$data = array(
'title' => 'Form View Page',
'record' => $this->Admin_model->getSubmissionById($form_submission_id)
);
$this->load->view('admin/view/index', $data);
$this->load->view('template/footer');
}
}
Model
class Admin_model extends CI_Model {
public function getSubmissionById($form_submission_id) {
$this->db->select('*');
$this->db->from('submissions');
$this->db->where('id', $form_submission_id);
$query = $this->db->get();
return $query->row();
}
}
Routes
// redirect any urls that contain a number after /form
$route['admin/view/form/(:num)'] = "admin/view/$1";
My views folder structure looks like this;
- views
-- admin
- index.php
- view
-- index.php
Ideally I want my URLs to look like this, and throw 404 errors where they don't.
example.com/admin/view/form/123
example.com/admin/update/form/123
example.com/admin/delete/form/123
You can do this by several ways.
The fastest way is by creating routes like this:
$route['admin/view/form/(:num)'] = "Admin/view/$1";
When typing exemple.com/admin/view/form/9 , it will redirect to the controller admin, function view and the "9" will be the parameter $form_submission_id
The same thing for routes below.
$route['admin/update/form/(:num)'] = "Admin/update/$1";
$route['admin/delete/form/(:num)'] = "Admin/delete/$1";
of course you could create a function for each one, view, update, delete ... etc
It's normal that you see error when you enter example.com/admin/view because the view function take 1 parameter $form_submission_id

Redirecting to a page in laravel

I wants to make a list page and add page
for list page my route function is
Route::get('admin/auctionlist','AdminController#showAuctionList');
and controller is
public function showAuctionList(){
$auctions = DB::table('auctionitems')
->leftjoin('campaigns','campaigns.id','=','auctionitems.campId')
->select('auctionitems.*','campaigns.title')
->get();
return View::make('admin/auctionlist')->with('auction',$auctions);
}
it works fine and my url is
http://localhost/vishal/site/public/admin/auctionlist
And for my add page route is
Route::post('addAuction',function(){
$obj = new AdminController() ;
return $obj->addAuction();
});
controller is
public function addAuction(){
AuctionModel::addAuctions(Input::except(array('_token')));
return $this->showAuctionList();
}
It redirects to list page but url showing as
http://localhost/vishal/site/public/addAuction
Aucually i want to the url as
http://localhost/vishal/site/public/admin/auctionlist
how can i get it.?
Finally i solved the issue.
public function addAuction(){
AuctionModel::addAuctions(Input::except(array('_token')));
return Redirect::to('admin/auctionlist');
}
its in laravel 4.
in laravel 5
it should be
return $this->redirect()->route('some-route-name');
Just use return redirect. And why messing your routing with functions? Just rule it to use the function in your controller:
routing
Route::post('admin/auctionlist','AdminController#addAuction');
controller
public function addAuction(){
AuctionModel::addAuctions(Input::except(array('_token')));
return redirect('admin/auctionlist');
}

CI routing issue

i am currently learning CI and i have come to an issue that i cant seem to solve.
i set up my wamp server in a drive and inside the www(root) folder i have extracted the codeigniter files.
![example][1][1]: http://i.stack.imgur.com/7RKqG.png
then I created my php files for view/model and controller and set the default route in the config/routes.php
so now when I go to my browser and type localhost I get the post.php displayed without anyissue.
but I am unable to access any of the views from here. for example i have a new_post.php view and when i type in the address bar localhost/new_post.php i get a "Not Found
The requested URL /new_post.php was not found on this server." error.
what am i doing wrong? below i have posted the code which i have written in the post.php controller along with a image of the file structure/names i have.
posts.php - controller
<?php
class Posts extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('post'); //loads the post model u created in the models folder
}
function index() //goes to this function 1st when u access the controller
{
$data['posts']=$this->post->get_posts(); // load all the data from the get_posts function in post model to the data array posts
$this->load->view('post_index', $data); //loads the view
}
function post($postID)
{
$data['post']=$this->post->get_post($postID);
$this->load->view('post', $data);
}
function new_post()
{
if($_POST)
{
$data=array(
'title'=> $_POST['title'],
'post'=> $_POST['post'],
'active' =>1
);
$this->post->insert_post($data);
redirect(base_url(). 'posts/');
}
else
{
$this->load->view('new_post');
}
}
function editpost($postID)
{
$data['success']=0;
if($_POST)
{
$data_post=array(
'title'=> $_POST['title'],
'post'=> $_POST['post'],
'active' => 1
);
$this->post->update_post($postID,$data);
$data['success'] =1;
}
$data['post']=$this->post->get_post($postID);
$this->load->view('edit_post',$data);
}
function deletepost($postID)
{
$this->post->delete_post($postID);
redirect(base_url(). 'posts/');
}
}
![structure][1] [1]: http://i.stack.imgur.com/SnsbW.png
In CodeIgniter you have to use controller to get access to his function
you are saying "when i type in the address bar localhost/new_post.php i get a Not Found" because you try to direct access its function name you have to use example.com/controllername/functionname like this
http://localhost/posts/new_post
for more information check codeignier url
https://ellislab.com/codeigniter/user-guide/general/urls.html
if you not removed index.php using .htaccess then you have to use your url like this
http://localhost/index.php/posts/new_post
In CodeIgniter, everything runs through the main "index.php" file, in your root directory.
So, you would access your new post page, like this;
http://localhost/index.php/posts/new_post
Have a read through the CodeIgniter user guide, it will answer any problem you have.
https://ellislab.com/codeigniter/user-guide/

Categories